【LeetCode】415. Add Strings 解题报告(Python)
作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/
[LeetCode]
题目地址:https://leetcode.com/problems/add-strings/
- Difficulty: Easy
题目描述
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.
题目大意
不使用大整数的情况下,计算两个字符串表示的数字的和。
解题方法
我的想法出奇的直白,就是模仿小学学的两个数的相加,从末尾开始向前以次相加,注意进位,两个数的相加最多只能进1,因此只要一个boolean 型的量表示是否进位即可。
提前进行补零,这样能够使得两个数字长度一样,方便了计算。
class Solution(object):
def addStrings(self, num1, num2):
"""
:type num1: str
:type num2: str
:rtype: str
"""
res = ""
carry = 0
M, N = len(num1), len(num2)
if M < N:
num1 = "0" * (N - M) + num1
else:
num2 = "0" * (M - N) + num2
N = max(M, N)
for i in range(N - 1, -1, -1):
add = int(num1[i]) + int(num2[i]) + carry
if add >= 10:
carry = 1
add -= 10
else:
carry = 0
res = str(add) + res
if carry:
res = "1" + res
return res
注意这几点:
- 两个数的位数保持一致才可。
- 加法的顺序是从个位向前
- 提前计算好两个数字的位数的差,否则在相加的时候会有变化。
Java代码如下。
public class Solution {
public String addStrings(String num1, String num2) {
int length = 0;
int sub = 0;
StringBuilder buffer1 = new StringBuilder(num1);
StringBuilder buffer2 = new StringBuilder(num2);
if (buffer1.length() > buffer2.length()) {
length = buffer1.length();
sub = buffer1.length() - buffer2.length();
} else {
length = buffer2.length();
sub = buffer2.length() - buffer1.length();
}
StringBuilder answer = new StringBuilder();
if (buffer1.length() > buffer2.length()) {
for (int i = 0; i < sub; i++) {
buffer2.insert(0, 0);
}
} else {
for (int i = 0; i < sub; i++) {
buffer1.insert(0, 0);
}
}
boolean up = false;
for (int i = length - 1; i >= 0; i--) {
int add = buffer1.charAt(i) - '0' + buffer2.charAt(i) - '0';
if (up) {
add++;
}
if (add >= 10) {
answer.insert(0, add - 10);
up = true;
} else {
answer.insert(0, add);
up = false;
}
if (i == 0 && up) {
answer.insert(0, 1);
}
}
return answer.toString();
}
}
AC: 38 ms
日期
2017 年 1 月 12 日
2018 年 11 月 19 日 —— 周一又开始了
【LeetCode】415. Add Strings 解题报告(Python)的更多相关文章
- [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 字符串相加
Given two non-negative numbers num1 and num2 represented as string, return the sum of num1 and num2. ...
- LeetCode 258 Add Digits 解题报告
题目要求 Given a non-negative integer num, repeatedly add all its digits until the result has only one d ...
- 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】120. Triangle 解题报告(Python)
[LeetCode]120. Triangle 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址htt ...
- 【leetcode】415. Add Strings
problem 415. Add Strings solution: class Solution { public: string addStrings(string num1, string nu ...
- LeetCode 1 Two Sum 解题报告
LeetCode 1 Two Sum 解题报告 偶然间听见leetcode这个平台,这里面题量也不是很多200多题,打算平时有空在研究生期间就刷完,跟跟多的练习算法的人进行交流思想,一定的ACM算法积 ...
随机推荐
- 解决Gitlab的The remote end hung up unexpectedly错误,解决RPC failed; HTTP 413 curl 22 The requested URL returned error: 413 Request Entity Too Large问题
解决Gitlab的The remote end hung up unexpectedly错误 解决RPC failed; HTTP 413 curl 22 The requested URL retu ...
- web性能测试工具——http_load
http_load是一款基于Linux平台的web服务器性能测试工具,用于测试web服务器的吞吐量与负载,web页面的性能. http_load是基于linux.unix平台的一种性能测工具 它以并行 ...
- jquery时间轴tab切换效果实现结合swiper实现滑动显示效果
需求:根据时间轴进行tab页面内容切换(时间轴需要滑动查看并选择) 实现思路: 结合swiper插件实现滑动显示效果 根据transform: translateX进行左侧切换效果的实现(具体实现cs ...
- Python | 迭代器与zip的一些细节
首先抛出一个困扰本人许久的问题: nums = [1,2,3,4,5,6] numsIter = iter(nums) for _ in zip(*[numsIter]*3): print(_) pr ...
- javaWeb - 1 — servlet — 更新完毕
1.先来聊一些javaWeb相关的知识 简单了解一下:web的发展史 1).web就是网页的意思嘛 2).web的分类 (1).静态web 使用HTML.CSS技术,主要包括图片和文本 优点:简单,只 ...
- 学习java的第八天
一.今日收获 1.学习完全学习手册上2.3转义字符与2.4运算符两节 二.今日难题 1.没有什么难理解的问题 三.明日目标 1.哔哩哔哩教学视频 2.Java学习手册
- 日常Java 2021/11/2
Java提供了一种对象序列化的机制,该机制中,一个对象可以被表示为一个字节序列,该字节序列包括该对象的数据.有关对象的类型的信息和存储在对象中数据的类型.将序列化对象写入文件之后,可以从文件中读取出来 ...
- treeTable实现排序
/* * * TreeTable 0.1 - Client-side TreeTable Viewer! * @requires jQuery v1.3 * * Dual licensed under ...
- 【Linux】【Commands】文本查看类
分屏查看命令:more和less more命令: more FILE 特点:翻屏至文件尾部后自动退出: less命令: less FILE head命令: 查看文件的前n行: head [option ...
- js和jquery之间的转换
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...