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. 【vue学习】vue 2.0版本以上创建项目的的步骤

    一.环境准备 1.vue项目依赖 node.js npm,需要先安装node和npm,先检查本地是否安装node.npm 快捷键win+r   输入cmd  弹出操作框,如果电脑已经安装git,直接右 ...

  2. js的关于for的语句

    JavaScript for...in 语句 for...in 语句用于对数组或者对象的属性进行循环操作. for ... in 循环中的代码每执行一次,就会对数组的元素或者对象的属性进行一次操作. ...

  3. 《用Python写爬虫》学习笔记(二)编写第一个网络爬虫

    1.首先,下载网页使用Python的urllib2模块,或者Python HTTP模块request来实现 urllib2会出现问题,解决方法1.重试下载(设置下载次数) 2.设置用户代理 2.其次, ...

  4. Python2.X和Python3.X中Tkinter模块的文件对话框、下拉列表的不同

    Python2.X和Python3.X文件对话框.下拉列表的不同 今天初次使用Python Tkinter来做了个简单的记事本程序.发现Python2.x和Python3.x的Tkinter模块的好多 ...

  5. python 捕获异常顺序

    catch 异常的时候,有关的异常(若是抛出子类异常,则父类异常的except也算.反之不算)except的语句是按代码顺序执行, 也就是说,当一个异常发生时,从若干except中若遇见异常类基类,父 ...

  6. JQuery案例一:实现表格隔行换色

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

  7. angular.js 渲染

    angular.js 小常识   具体看代码,转载请备注来源. html结构 <%@ page language="java" contentType="text/ ...

  8. python的列表生成式

    列表生成式 列表生成式即List Comprehensions,是Python内置的非常简单却强大的可以用来创建list的生成式. 举个例子,要生成list [1, 2, 3, 4, 5, 6, 7, ...

  9. 小白的REDIS学习(二)-链表

    本文为读<Redis设计与实现>的记录.该书以Redis2.9讲解Redis相关内容.请注意版本差异. Redis使用C语言,实现了自己的链表结构,实现的代码如下 //集成了链表的各类信息 ...

  10. Tomcat配置及不依赖IDEA部署web应用

    http:tomcat.apache.org 下载tomcat文件包 我使用的tomcat9的版本 Tomcat9014使用的是Servlet4.0 解压即可,目录如下 bin :启动和关闭tomca ...