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. Win10安装LoadRunner11

    一.下载 地址:http://www.51testing.com/?uid-4827-action-viewspace-itemid-225451 二.安装 本来想写,结果和别人的一样就不写了:htt ...

  2. java 8 Lambda

    警告: 初学者随笔, 请关闭此网页, 以免浪费你的时间

  3. Vue 插槽

    插槽的概念: 插槽的关键字slot,默认情况下,组件中的模板会覆盖组件中的原始内容(即自定义标签对内部的内容会不显示),解决办法就是使用插槽. 组件的原始内容: 即在vue实例范围之内,因此可以调用实 ...

  4. 导航菜单点击图片切换--jquery

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  5. php实现遍历目录

    用递归方法实现目录的遍历: <?php header("Content-type: text/html; charset=utf-8"); date_default_time ...

  6. 比较爬虫用的语言Python与Go

    Python是我比较喜欢的语言,莫名的喜欢,对Python的学习可能起初是敲错了网址开始的,哈哈哈~ 工作的任务从一个网站后台做登录.爬取数据,写入服务器Redis中,同事认为我会用PHP来写,哼!让 ...

  7. Python随笔--魔法方法(析构与构造)

    #析构方法的调用

  8. 初读"Thinking in Java"读书笔记之第五章 --- 初始化与清理

    用构造器确保初始化 构造器可以确保每个对象都会得到初始化,Java毁在创建对象时自动调用构造器. 构造器采用与类名相同的名称,因此并不适合"每个方法首字母小写的风格". 构造器默认 ...

  9. cocso引擎整体流程

    任何程序都有入口,mian.cpp; Cocos2d也不免俗,在win32平台下,有一个mian.cpp 入口,从这里进入cocos的世界. #ifndef __MAIN_H__ #define __ ...

  10. 运行caffe自带的mnist实例教程

    运行caffe自带的mnist实例教程 本文结合几篇博文总结下来的,附上其中一篇原博文链接以供参考:http://blog.sina.com.cn/s/blog_168effc7e0102xjr1.h ...