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
}
相关推荐
Fly Wine3 小时前
Leetcode之有效字母异位词
算法·leetcode·职场和发展
lars_lhuan4 小时前
Go WaitGroup 源码解析
golang
程序员夏末5 小时前
【LeetCode | 第七篇】算法笔记
笔记·算法·leetcode
hanlin037 小时前
刷题笔记:力扣第43、67题(字符串计算)
笔记·算法·leetcode
人间打气筒(Ada)8 小时前
如何基于 Go-kit 开发 Web 应用:从接口层到业务层再到数据层
开发语言·后端·golang
We་ct10 小时前
LeetCode 34. 在排序数组中查找元素的第一个和最后一个位置:二分查找实战
前端·算法·leetcode·typescript·二分
ccLianLian11 小时前
leetcode-hot100
算法·leetcode·职场和发展
nainaire11 小时前
速通LeetCode hot100——(1~9 哈希,双指针,滑动窗口)
c++·笔记·算法·leetcode
XiYang-DING12 小时前
【LeetCode】LCR 019. 验证回文串 II
算法·leetcode·职场和发展
灰色小旋风12 小时前
力扣18 四数之和(C++)
数据结构·算法·leetcode