Golang | Leetcode Golang题解之第415题字符串相加

题目:

题解:

Go 复制代码
func addStrings(num1 string, num2 string) string {
    add := 0
    ans := ""
    for i, j := len(num1) - 1, len(num2) - 1; i >= 0 || j >= 0 || add != 0; i, j = i - 1, j - 1 {
        var x, y int
        if i >= 0 {
            x = int(num1[i] - '0')
        }
        if j >= 0 {
            y = int(num2[j] - '0')
        }
        result := x + y + add
        ans = strconv.Itoa(result%10) + ans
        add = result / 10
    }
    return ans
}
相关推荐
VT.馒头1 天前
【力扣】2694. 事件发射器
前端·javascript·算法·leetcode·职场和发展·typescript
好学且牛逼的马1 天前
【Hot100|21-LeetCode 160. 相交链表】
算法·leetcode
Remember_9931 天前
Spring 事务深度解析:实现方式、隔离级别与传播机制全攻略
java·开发语言·数据库·后端·spring·leetcode·oracle
练习时长一年1 天前
LeetCode热题100(颜色分类)
算法·leetcode·职场和发展
We་ct1 天前
LeetCode 73. 矩阵置零:原地算法实现与优化解析
前端·算法·leetcode·矩阵·typescript
好学且牛逼的马1 天前
【Hot100|22-LeetCode 206. 反转链表 - 完整解法详解】
算法·leetcode·矩阵
v_for_van1 天前
力扣刷题记录3(无算法背景,纯C语言)
c语言·算法·leetcode
We་ct1 天前
LeetCode 48. 旋转图像:原地旋转最优解法
前端·算法·leetcode·typescript
爱尔兰极光1 天前
LeetCode--长度最小的子数组
算法·leetcode·职场和发展