首页 > 其他 > 详细

力扣415. 字符串相加

时间:2021-01-24 21:46:04      阅读:34      评论:0      收藏:0      [点我收藏+]

原题

 1 class Solution:
 2     def addStrings(self, num1: str, num2: str) -> str:
 3         ans = ""
 4         i,j,carry=len(num1)-1,len(num2)-1,0
 5         while i >=0 or j >= 0:
 6             s = carry
 7             if i >= 0:
 8                 s += int(num1[i])
 9                 i -= 1
10             if j >= 0:
11                 s += int(num2[j])
12                 j -= 1
13             carry = s // 10
14             ans += str(s % 10)
15         ans += 1 if carry == 1 else ‘‘
16         return ans[::-1]

 

力扣415. 字符串相加

原文:https://www.cnblogs.com/lj95/p/14322045.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!