问题描述:

给定两个字符串形式的非负整数 num1num2 ,计算它们的和。

注意:

  1. num1num2 的长度都小于 5100.
  2. num1num2 都只包含数字 0-9.
  3. num1num2 都不包含任何前导零。
  4. 你不能使用任何內建 BigInteger 库, 也不能直接将输入的字符串转换为整数形式。

方法:

 class Solution(object):
def addStrings(self, num1, num2):
"""
:type num1: str
:type num2: str
:rtype: str
"""
res = ""
c = 0
i , j = len(num1) -1,len(num2) - 1
while i >= 0 or j >= 0:
if i >= 0:
c += ord(num1[i]) - ord('')
i -= 1
if j >= 0:
c += ord(num2[j]) - ord('')
j -= 1
res = str(c % 10) + res
c //= 10
if c:
res = str(c) + res
return res

2018-10-01 09:04:24

最新文章

  1. weex scroller
  2. marquee 标签 文字滚动
  3. JAVA递归算法
  4. android获取string.xml的值(转)
  5. WebService 实现BS环境与BS环境传递参数,根据参数生成txt文档
  6. Windows 下启动Apache服务(转)
  7. printf 缓冲区问题
  8. C语言使用正则表达式
  9. phpadmin
  10. ASP.NET MVC 音乐商店 - 目录
  11. php将中文插入数据库出现乱码
  12. WPF依赖属性2
  13. 面试题中遇到的算法与js技巧
  14. Java中浮点数的精度问题 【转】
  15. async/await套路编程
  16. maven dependencies
  17. Python开发【笔记】:获取目录下所有文件
  18. commit 流程
  19. IAR EWARM : Debugging with CMSIS-DAP
  20. Redis的Docker镜像

热门文章

  1. java中读取配置文件的方法
  2. Python3 tkinter基础 Listbox delete 删除单个、所有元素
  3. (转)Redis & EhCache
  4. Linux/shell: remove adjacent similar patterns
  5. 题解——loj6279 数列分块入门3 (分块)
  6. Optical Flow related Tutorials
  7. Docker、Kubenets使用前配置
  8. 测试与CMMI质量体系
  9. jQuery中的$(window).load()与$(document).ready()以及jquery $(document).ready() 与window.onload的区别
  10. repr() 和 str() 函数