[LeetCode&Python] Problem 415. Add Strings
Given two non-negative integers num1 and num2 represented as string, return the sum of num1 and num2.
Note:
- The length of both
num1andnum2is < 5100. - Both
num1andnum2contains only digits0-9. - Both
num1andnum2does not contain any leading zero. - 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的更多相关文章
- [LeetCode&Python] Problem 258. Add Digits
Given a non-negative integer num, repeatedly add all its digits until the result has only one digit. ...
- 【leetcode】415. Add Strings
problem 415. Add Strings solution: class Solution { public: string addStrings(string num1, string nu ...
- [LeetCode] 415 Add Strings && 67 Add Binary && 43 Multiply Strings
这些题目是高精度加法和高精度乘法相关的,复习了一下就做了,没想到难住自己的是C++里面string的用法. 原题地址: 415 Add Strings:https://leetcode.com/pro ...
- 36. leetcode 415. Add Strings
415. Add Strings Given two non-negative integers num1 and num2 represented as string, return the sum ...
- 【LeetCode】415. Add Strings 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 [LeetCode] 题目地址:https:/ ...
- [LeetCode] 415. Add Strings 字符串相加
Given two non-negative numbers num1 and num2 represented as string, return the sum of num1 and num2. ...
- LeetCode - 415. Add Strings
Given two non-negative numbers num1 and num2 represented as string, return the sum of num1 and num2. ...
- [leetcode]415. Add Strings字符串相加
Given two non-negative integers num1 and num2 represented as string, return the sum of num1 and num2 ...
- [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 ...
随机推荐
- Promise 错误处理
Promise 是一个异步返回单个结果的函数或方法 不使用 `catch()` 时,在 `success handler` 里的错误无法被捕捉到 使用 `catch()` 时,在 `succe ...
- 安装easydict
在运行lightheadrcnn做test时,提示缺少easydict 不知道什么原因,用pip install easydict或者conda install easydict都没有用,不能安装ea ...
- perceptual loss
https://arxiv.org/abs/1603.08155 两个网络:image transfer网络和loss网络 image transfer网络: 将输入图片y通过映射f W (x)得到输 ...
- form表单提交到Servlet后,弹出对话框,然后在跳转页面
在Servlet中添加一下代码即可 out.print("<script>alert('添加成功!');window.location='index.jsp';</scri ...
- Javase系列之面向对象(一)
作为一个Java程序员,我们每天做的事情就是OOP(面向对象),可以说万物皆对象,Java是一门面向对象的程序语言,鉴于基本的面向对象知识也是一个较为庞杂的模块,所以博主我准备用多篇文章去介绍Java ...
- js中关于声明提前的几个误区
声明提前: 在程序正式执行之前,都会将所有的var声明的变量提前到开始位置,集中创建,而赋值留在原地. 例如这样一段代码 console.log(a) var a = 100; console.log ...
- SQL语句:如何让字符串转化数字
和前端联调的时候,突然出现一个状况,新增数据的时候,一直报系统错误,写下此文,留以后反复温习.菜鸟程序员一名~ 项目内容:新增产品信息 具体实现:1 获取基础信息,创建产品(调用接口传入的产品类型,如 ...
- Exploit-Exercises nebule 旅行日志(六)
接着上次的路程继续在ubuntu下对漏洞的探索练习,这次是level05了 先看下level05的问题描述: 从level05的描述上看,是/home/flag05目录的权限有漏洞,看来多半是又跟fl ...
- Linux文件打包与解压缩
一.文件打包和解压缩 常用的压缩包文件格式.在 Windows 上我们最常见的不外乎这三种*.zip,*.rar,*.7z后缀的压缩文件,而在 Linux 上面常见常用的除了以上这三种外,还有*.gz ...
- Charles篡改请求,在手机上抓包,以及弱网设置
篡改请求 可以测试各种异常 原理:clint->server正常是客户端发送请求到服务端,charles相当于一个拦截器,拦住客户端的请求,并进行修改,修改后再发送到server端 Server ...