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. 通信技术以及5G和AI保障电网安全与网络安全

    摘 要:电网安全是电力的基础,随着智能电网的快速发展,越来越多的ICT信息通信技术被应用到电力网络.本文分析了历史上一些重大电网安全与网络安全事故,介绍了电网安全与网络安全.通信技术与电网安全的关系以 ...

  2. 针对小程序for循环绑定数据,实现toggle切换效果(交流QQ群:604788754)

    如有更好的方法实现,可以留言或加群交流学习.谢谢(交流QQ群:604788754) WXML: <block wx:for="{{datanum}}" wx:for-inde ...

  3. ElasticsearchCRUD翻译系列之(一): ElasticsearchCRUD 介绍

    ELASTICSEARCH CRUD .NET PROVIDER 翻译自(原文地址: https://damienbod.com/2014/09/22/elasticsearch-crud-net-p ...

  4. Trojan.Backdoor分析

    总结:这是一个HTTP的后门,以安装(-in)||移除(-re)||配置(-c)为目的运行此程序时, 必须指定abcd为最后一个参数. 安装时他会把自身拷贝到%SYSTEMROOT%\WINDOWS\ ...

  5. unity 中UGUI制作滚动条视图效果(按钮)

    1.在unity中创建一个Image作为滚动条视图的背景: 2.在Image下创建一个空物体,在空物体下创建unity自带的Scroll View组件: 3.对滑动条视图的子物体进行调整: 4.添加滚 ...

  6. SpringBoot Actuator

    SpringBoot Actuator 提供了检查项目内部信息的一整套API,通常在项目启动时可以看到. 1.引入依赖包 <dependency> <groupId>org.s ...

  7. Linux SSH登录服务器报ECDSA host key "ip地址" for has changed and you have requested strict checking错误

    错误:ECDSA host key "ip地址" for  has changed and you have requested strict checking. 解决方案:在终端 ...

  8. 第一个HTML文档

    属性 和 值 1.作用 用来修饰元素 ex:让 p 标记的文本水平居中对齐 <p>Hello World</p> 2.语法      1.属性的声明必须位于开始标记里      ...

  9. 小程序 web-view 嵌套的网页跳转到小程序内部页面 实现无缝连接

    需要在H5页面被作出判断和处理  点击事件发生时跳转到小程序内部页面 1.引入小程序提供的JS <script type="text/javascript" src=&quo ...

  10. Linux 系统运行命令 > 查看系统信息

    查看系统运行状态 一 . 查看硬件信息 - 1. cpu信息(可以通过find,whereis,locate查出路径) #cat /proc/cpuinfo 2 . 内存信息:meminfo(可以用c ...