[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
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.
这个题目跟[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 ...
随机推荐
- 深入理解 Neutron -- OpenStack 网络实现(2):VLAN 模式
问题导读 1.br-int.br-ethx的作用是什么?2.安全组策略是如何实现的?3.VLAN 模式与GRE模式有哪些不同点?流量上有哪些不同?4.L3 agent实现了什么功能? 接上篇深入理解 ...
- java学习之导出Excel
1. 输出表格 poi输出excel最基本是输出table表格,下面是输出区域.总销售额(万元).总利润(万元)简单的表格,创建HSSFWorkbook 对象,用于将excel输出到输出流中 HSSF ...
- if中的-n -z linux_Shell
==========1 混淆的-n -z================= -n 表示这个变量或者字符串是否不为空.-z 表示这个变量或者字符串为空 上面这两句话中最重要的点是不通的 -n 关注的是 ...
- CentOS6.4挂载读写NTFS分区 (重要)
如今很多的linux衍生版本已经支持自动连接NTFS分区了,之前在一次安装的过程中,遇到 linux不能识别NTFS分区,解决方法如下文: ============================== ...
- MAC ox下配置mysql
下载MySQL安装包:http://dev.mysql.com/downloads/mysql/ 如图,选择dmg包,下载完成,双击后打开对应的pkg包,一直点击下一步,直到安装成功 启动和停止MyS ...
- 关于ASP.NET Web API的ModelBinding杂谈
由于客户端调用Web API传递的数据属性命名一般偏向javascript规范,只是简单的大小写差异没有问题,但始终会有一些特殊情况.比如OAuth的请求: client_id : "val ...
- PHPStorm 注册码&主题皮肤
JetBrains PhpStorm 注册方法: 用浏览器打开 http://idea.lanyus.com/ 点击页面中的“获得注册码” 然后打开PhpStorm,在注册时切换至Activation ...
- 一个js文件如何加载另外一个js文件
方法一,在调用文件的顶部加入下例代码: document.write(”<script language=javascript src=’/js/import.js’></scrip ...
- Twig---和vue或angular前端框架并存
<h1> {% verbatim %} {{message}} {% endverbatim %} </h1> 上面这种方式虽然能够解决,前台渲染的问题,但是还是会报错: 第二 ...
- Android-Broadcast(广播)
1.四大组件之BroadcastReceiver (1)Broadcast(广播)--一种广泛运用在应用程序之间传输信息的机制 (2)BroadcastReceiver(广播接受者)--是对发送过 ...