[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 ...
随机推荐
- 【Mybatis】Mybatis元素生命周期
一.SqlSessionFactoryBuilder SqlSessionFactoryBuilder是利用XML或者Java编码获得资源来构建SqlSessionFactory的,通过它可以构建多个 ...
- Android 请求运行时权限
写文件到sd卡中,会报权限问题,需要动态申请申请运行时权限 1. MainActivity.java public class MainActivity extends Activity { priv ...
- source.android.google && developer.android.google
https://source.android.google.cn/ https://developer.android.google.cn/ https://source.android.com/co ...
- LeetCode 23 Merge k Sorted Lists(合并k个有序链表)
题目链接: https://leetcode.com/problems/merge-k-sorted-lists/?tab=Description Problem: 给出k个有序的list, 将其进行 ...
- Mac Lion Configure Apache
mkdir ~/Sites echo "<html><body><h1>My site works</h1></body>< ...
- C# 日志系统 log4net 配置及使用
1.引用Dll 版本是:1.2.10.0,下载Dll 2.Web.config文件配置 <?xml version="1.0" encoding="utf-8&qu ...
- javap(反汇编命令)详解
javap是JDK自带的反汇编器,可以查看java编译器为我们生成的字节码.通过它,我们可以对照源代码和字节码,从而了解很多编译器内部的工作. 语法: javap [ 命令选项 ] class. . ...
- geotrellis使用(三十二)大量GeoTiff文件实时发布TMS服务
前言 在上一篇文章中我讲了如何直接将Geotiff文件发布为TMS服务,在其中只讲了单幅Geotiff的操作,其实单幅这种量级的数据对Geotrellis来说就是杀鸡焉用牛刀,Geotrellis针对 ...
- 兵器簿之Alcatraz(插件管理神器)的配置和使用
Alcatraz是一款开源框架,帮助我们管理和安装Xcode需要的一些插件,很赞,安装也很简单: 终端输入: curl -fsSL https://raw.githubusercontent.com/ ...
- wpgcms---碎片管理的使用
这里很神奇的是碎片管理是编辑器,所以拿到的配置都是富文本,所以在前台作为字段来使用的时候,需要过滤掉字符串. 具体示例: {% set qq = wpg.fragment.get("qq&q ...