[LeetCode] 415. Add Strings_Easy tag: String
Given two non-negative integers num1
and num2
represented as string, return the sum of num1
and num2
.
Note:
- The length of both
num1
andnum2
is < 5100. - Both
num1
andnum2
contains only digits0-9
. - Both
num1
andnum2
does not contain any leading zero. - You must not use any built-in BigInteger library or convert the inputs to integer directly.
这个题目跟[LeetCode] 67. Add Binary_Easy tag: String思路一致, 先将length补齐, 然后进位, 最后判断edge case, 如果有进位的处理, 然后这里用ord(a[i]) - ord('0') 转换integer.
Code
class Solution:
def addStrings(self, num1, num2):
m, n = len(num1), len(num2)
if m > n:
num1, num2, m, n = num2, num1, n, m
num1, pre, ans = ''*(n-m) + num1, 0, ""
for i in range(n)[::-1]:
pre, rem = divmod(ord(num1[i]) + ord(num2[i]) - 2* ord('') + pre, 10)
ans += str(rem)
return ans[::-1] if not pre else '' + ans[::-1]
[LeetCode] 415. Add Strings_Easy tag: String的更多相关文章
- [LeetCode] 67. Add Binary_Easy tag: String
Given two binary strings, return their sum (also a binary string). The input strings are both non-em ...
- [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 616. Add Bold Tag in String
原题链接在这里:https://leetcode.com/problems/add-bold-tag-in-string/description/ 题目: Given a string s and a ...
- [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] 258. Add Digits_Easy tag: Math
Given a non-negative integer num, repeatedly add all its digits until the result has only one digit. ...
- [leetcode]415. Add Strings字符串相加
Given two non-negative integers num1 and num2 represented as string, return the sum of num1 and num2 ...
- 【leetcode】415. Add Strings
problem 415. Add Strings solution: class Solution { public: string addStrings(string num1, string nu ...
随机推荐
- Javascript常见性能优化
俗话说,时间就是生命,时间就是金钱,时间就是一切,人人都不想把时间白白浪费,一个网站,最重要的就是体验,而网站好不好最直观的感受就是这个网站打开速度快不快,卡不卡. 当打开一个购物网站卡出翔,慢的要死 ...
- 原生js--类、原型、构造函数
1.类和原型:原型对象是类的核心,类的所有实例都从同一个原型上继承属性和方法,原型对象是类的唯一标识 2.类和构造函数:构造函数通过new关键字来创建对象,构造函数的prototype属性被用作新对象 ...
- 深入学习Make命令和Makefile(上)
https://www.zybuluo.com/lishuhuakai/note/209302 深入学习Make命令和Makefile(上) make是Linux下的一款程序自动维护工具,配合make ...
- 如何区分slice、splice和split
小颖之前写过一篇文章:JavaScript Array 对象方法 以及 如何区分javascript中的toString().toLocaleString().valueOf()方法中有分享过slic ...
- nginx 启动重启脚本
#! /bin/sh # Default-Start: 2 3 4 5 # Default-Stop: 0 1 6 # Short-Description: starts the n ...
- AD 文档信息设置和制作模板
原理图文档模板制作方法一.在DXP2004/AD6.0/AD6.3原理图设计环境下,新建一个自由原理图文档.单击:文件→新建→原理图,或者使用快捷键Ctrl+N打开Files资源面板,在“新建”项目下 ...
- Docker定制容器镜像(利用Dockerfile文件)
1.创建Dockerfile文件 新建一个目录,在里面新建一个dockerfile文件(新建一个的目录,主要是为了和以防和其它dockerfile混乱 ) [root@docker01 myfiles ...
- Redis学习资料整理
Redis学习资料: (1)Redis设计与实现 (2)十五分钟介绍 Redis数据结构 (3)redis安装 (4)redis指令手册中文版 Hiredis学习资料: (1)hiredis安装及测试 ...
- wpgcms---碎片管理的使用
这里很神奇的是碎片管理是编辑器,所以拿到的配置都是富文本,所以在前台作为字段来使用的时候,需要过滤掉字符串. 具体示例: {% set qq = wpg.fragment.get("qq&q ...
- 火狐浏览器adobe flash player
最近Firefox火狐浏览器已停止Flash Player 18.0.0.203运行 在Mozilla官方公告中,该公司提到Flash Player 18.0.0.203插件存在已知安全漏洞,建议用户 ...