Given two non-negative integers num1 and num2 represented as string, return the sum of num1 and num2.

Note:

  1. The length of both num1 and num2 is < 5100.
  2. Both num1 and num2 contains only digits 0-9.
  3. Both num1 and num2 does not contain any leading zero.
  4. You must not use any built-in BigInteger library or convert the inputs to integer directly.
class Solution(object):
def addStrings(self, num1, num2):
"""
:type num1: str
:type num2: str
:rtype: str
"""
def str2num(i):
if i=='0':
return 0
elif i=='1':
return 1
elif i=='2':
return 2
elif i=='3':
return 3
elif i=='4':
return 4
elif i=='5':
return 5
elif i=='6':
return 6
elif i=='7':
return 7
elif i=='8':
return 8
elif i=='9':
return 9 def num2str(i):
if i==0:
return '0'
elif i==1:
return '1'
elif i==2:
return '2'
elif i==3:
return '3'
elif i==4:
return '4'
elif i==5:
return '5'
elif i==6:
return '6'
elif i==7:
return '7'
elif i==8:
return '8'
elif i==9:
return '9' addone=0
ans=""
num1=num1[::-1]
num2=num2[::-1]
if len(num2)>len(num1):
temp=num1
num1=num2
num2=temp
for i in range(len(num1)):
if i<len(num2):
n1=str2num(num1[i])
n2=str2num(num2[i])
s=n1+n2+addone
s0=s%10
ans+=num2str(s0)
if s>=10:
addone=1
else:
addone=0
else:
n1=str2num(num1[i])
s=n1+addone
s0=s%10
ans+=num2str(s0)
if s>=10:
addone=1
else:
addone=0
if addone==1:
ans+='1'
return ans[::-1]

  

[LeetCode&Python] Problem 415. Add Strings的更多相关文章

  1. [LeetCode&Python] Problem 258. Add Digits

    Given a non-negative integer num, repeatedly add all its digits until the result has only one digit. ...

  2. 【leetcode】415. Add Strings

    problem 415. Add Strings solution: class Solution { public: string addStrings(string num1, string nu ...

  3. [LeetCode] 415 Add Strings && 67 Add Binary && 43 Multiply Strings

    这些题目是高精度加法和高精度乘法相关的,复习了一下就做了,没想到难住自己的是C++里面string的用法. 原题地址: 415 Add Strings:https://leetcode.com/pro ...

  4. 36. leetcode 415. Add Strings

    415. Add Strings Given two non-negative integers num1 and num2 represented as string, return the sum ...

  5. 【LeetCode】415. Add Strings 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 [LeetCode] 题目地址:https:/ ...

  6. [LeetCode] 415. Add Strings 字符串相加

    Given two non-negative numbers num1 and num2 represented as string, return the sum of num1 and num2. ...

  7. LeetCode - 415. Add Strings

    Given two non-negative numbers num1 and num2 represented as string, return the sum of num1 and num2. ...

  8. [leetcode]415. Add Strings字符串相加

    Given two non-negative integers num1 and num2 represented as string, return the sum of num1 and num2 ...

  9. [LeetCode&Python] Problem 427. Construct Quad Tree

    We want to use quad trees to store an N x N boolean grid. Each cell in the grid can only be true or ...

随机推荐

  1. Spring中使用RedisTemplate操作Redis(spring-data-redis)

    RedisTemplate如何检查一个key是否存在? return getRedisTemplate().hasKey(key); 由一个问题,复习了一下redis 抄自: https://www. ...

  2. 苹果手机的SB系列(3)超级烦人的账户解锁?

    不知道大家有没有这种体验,Iphone 每隔一段时间后账户就被锁定了,也不告诉你原因,就是要解锁? 我怎么感觉比做的比支付宝差多了,我注册支付宝十几年,也没有动不动就告诉了有安全原因,要解锁,要重置密 ...

  3. django数据库的表已迁移的不能重新迁移的解决办法

    django.db.utils.InternalError: (1050, "Table 'tb_content' already exists") mysql数据库在迁移时数据库 ...

  4. java.lang.NumberFormaException: For input String:"user"

    碰到这个问题的时候有点懵逼,很无力,网上一查是类型转换错误,贴上报错的JSP代码 最后找救兵,得出是hql的错误,举个例子: HibernateUtil.query("from user u ...

  5. QPS的优化

    cdn加速 吧静态资源放到别人的服务器上 精灵图 后台数据库用mysql+redis sql的优化 用缓存 程序架构:集群化部署 ,分布式+异步     celery:分布式异步任务框架 语言

  6. Rhino学习教程——1.3

    在主工具栏的工作视窗配置一栏中,有一个背景图工具. 展开是: . 功能是: 放置背景图 移除背景图 移动背景图 缩放背景图 对齐背景图 隐藏背景图/显示背景图 用“图框平面”工具  也可以导入一个图片 ...

  7. Oracle数据库字段数据拆分成多行(REGEXP_SUBSTR函数)

    做多选功能时为了简便,会在某个字段中存储多个值,保存时虽然省事,但后续的查询统计时还需要拆分数据才行,因此这时需要将字段内的值分成多行以便后续使用. 下面这个例子实现了字段内数据的拆分: --创建测试 ...

  8. js 发送http请求

    // 1.创建 XHR对象(IE6- 为ActiveX对象) // 2.连接及发送请求 // 3.回调处理 function createXMLHttpRequest() {     var xhr; ...

  9. StringBuffer&StringBuilder

    对字符串修改时,用到StringBuffer&StringBuilder,能够多次修改对象并且不产生新的未使用对象 StringBuilder线程不安全(不能同步访问),速度有优势,多数情况下 ...

  10. 利用postman 实现Get和Post测试

    通过之前对金字塔结构的学习,大概了解到了金字塔模型想告诉我们的几个道理: 1.越底层,越稳定. 金字塔主要观点认为单元测试的稳定性高,需要多投入. 2.越底层,越高效. 程序的问题,最终还得落在具体的 ...