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
}
相关推荐
Joyner20185 小时前
python-leetcode-从中序与后序遍历序列构造二叉树
算法·leetcode·职场和发展
因兹菜5 小时前
[LeetCode]day9 203.移除链表元素
算法·leetcode·链表
LNsupermali5 小时前
力扣257. 二叉树的所有路径(遍历思想解决)
算法·leetcode·职场和发展
雾月555 小时前
LeetCode LCR180文件组合
算法·leetcode·职场和发展
萌の鱼5 小时前
leetcode 2080. 区间内查询数字的频率
数据结构·c++·算法·leetcode
Tisfy5 小时前
LeetCode 0541.反转字符串 II:模拟
算法·leetcode·字符串·题解
加油,旭杏6 小时前
【go语言】接口
开发语言·后端·golang
labmem18 小时前
Leetcode:541
算法·leetcode·职场和发展
圆圆滚滚小企鹅。8 小时前
刷题记录 HOT100回溯算法-6:79. 单词搜索
笔记·python·算法·leetcode
钓一朵雪10 小时前
【Leetcode刷题记录】45. 跳跃游戏 II--贪心算法
leetcode·贪心算法