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.

这个题目跟[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的更多相关文章

  1. [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 ...

  2. [LeetCode] 415 Add Strings && 67 Add Binary && 43 Multiply Strings

    这些题目是高精度加法和高精度乘法相关的,复习了一下就做了,没想到难住自己的是C++里面string的用法. 原题地址: 415 Add Strings:https://leetcode.com/pro ...

  3. 36. leetcode 415. Add Strings

    415. Add Strings Given two non-negative integers num1 and num2 represented as string, return the sum ...

  4. LeetCode 616. Add Bold Tag in String

    原题链接在这里:https://leetcode.com/problems/add-bold-tag-in-string/description/ 题目: Given a string s and a ...

  5. [LeetCode] 415. Add Strings 字符串相加

    Given two non-negative numbers num1 and num2 represented as string, return the sum of num1 and num2. ...

  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] 258. Add Digits_Easy tag: Math

    Given a non-negative integer num, repeatedly add all its digits until the result has only one digit. ...

  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】415. Add Strings

    problem 415. Add Strings solution: class Solution { public: string addStrings(string num1, string nu ...

随机推荐

  1. 开发常见错误之 : IMP-00058: 遇到 ORACLE 错误 1691

    IMP-00058: 遇到 Oracle 错误 1691ORA-01691: Lob 段YQPRO.SYS_LOB0000031467C00006$$无法通过128(在表空间YQPRO中)扩展这种情况 ...

  2. ELK系列二:Elasticsearch的架构原理和配置优化

    1.Elasticsearch的数据组织架构 1.1.Elasticsearch结构概念 集群(cluster):拥有相同cluster-name的elasticsearch结点的集合(每个结点其实就 ...

  3. (转载)解决AndroidStudio导入项目在 Building gradle project info 一直卡住

    源地址http://blog.csdn.net/yyh352091626/article/details/51490976 Android Studio导入项目的时候,一直卡在Building gra ...

  4. 基本类型算法题目学习(EPI)

    1.关于奇偶校验的方法中,如何快速的求取一个64-bit的数字的奇偶校验位.(如果1的位数为奇数,则奇偶校验位为1,如果1的位数为偶数,则奇偶校验位为0) a.暴力枚举法采用一位一位进行计算,一位一位 ...

  5. android 设置系统屏幕亮度

    /** * 获得当前屏幕亮度的模式 * SCREEN_BRIGHTNESS_MODE_AUTOMATIC=1 为自动调节屏幕亮度 * SCREEN_BRIGHTNESS_MODE_MANUAL=0 为 ...

  6. CmD空格转义的三种方法,总有一种会解决问题

    CmD空格转义 在cmd中,如果路径中存在空格报错 可以有三种解决方法: 1.将存在空格的路径用双引号包起来,如:"D:/Program Files/xx"; 2.将存在空格的名称 ...

  7. django的queryset和objects对象

    1. queryset是查询集,就是传到服务器上的url里面的内容.Django会对查询返回的结果集QerySet进行缓存,这里是为了提高查询效率. 也就是说,在你创建一个QuerySet对象的时候, ...

  8. 关于 Java 泛型的一些有趣的例子

    有以下的代码: try { ArrayList<String> lstA = new ArrayList<String>(); ArrayList<Integer> ...

  9. Linux查看及设置系统字符集

    查看正在使用的字符集 查看可以设置的字符集 locale -a 修改字符集 export LANG=zh_CN.gbk

  10. HOJ 2985 Wavio Sequence(最长递增子序列以及其O(n*logn)算法)

    Wavio Sequence My Tags (Edit) Source : UVA Time limit : 1 sec Memory limit : 32 M Submitted : 296, A ...