【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算法积 ...
随机推荐
- Dango之form校验组件
目录 1.引入案例 2. form组件的功能 3. form组件的使用 3.1 自定义form校验类 3.2 校验数据 3.3 渲染页面 3.4 展示错误信息 3.5 自定义校验结果 3.6 form ...
- 使用SpringBoot实现登录注册的几个问题
一.用户名密码都正确的情况下被登录拦截器拦截 控制台报错:org.apache.ibatis.executor.ExecutorException: A query was run and no Re ...
- .NET Core如何配置TLS Cipher(套件)?
前言 前不久我发表了一篇关于TLS协议配置被我钻了空子,经过第三方合作伙伴验证,针对此TLS协议存在不安全套件,急催速速解决,那么我们本篇开始继续整活!第三方合作伙伴对平台安全严苛要求,我们已连续发版 ...
- 01 eclipse搭建maven的web工程(3.1)
eclipse搭建maven的web工程(3.1) 一.下载并在eclipse安装JDK环境[查看] 二.下载并在eclipse安装maven环境[查看] 三.新建maven-webapp工程: 1. ...
- javaSE基础知识(走向编程的门口)— 更新完毕
前言:玩儿编程最重要的一点:不要怕麻烦,感觉是在浪费时间: 能动手绝不哔哔:只要脑袋不傻,编程都是"一看就会,一练就废",开始学的时候,就算再基础的东西都建议手敲一遍 要有囫囵吞枣 ...
- HashMap有几种遍历方法?推荐使用哪种?
本文已收录<面试精选>系列,Gitee 开源地址:https://gitee.com/mydb/interview HashMap 的遍历方法有很多种,不同的 JDK 版本有不同的写法,其 ...
- Netty之ByteBuf
本文内容主要参考<<Netty In Action>>,偏笔记向. 网络编程中,字节缓冲区是一个比较基本的组件.Java NIO提供了ByteBuffer,但是使用过的都知道B ...
- Set && Map
ES6 提供了新的数据结构 Set, Map Set成员的值都是唯一的,没有重复的值,Set内的元素是强类型,会进行类型检查. let set = new Set([1, true, '1', 'tr ...
- 【leetcode】952. Largest Component Size by Common Factor(Union find)
You are given an integer array of unique positive integers nums. Consider the following graph: There ...
- Dubbo多协议支持
除了Dubbo服务暴露协议Dubbo协议外,Dubbo框架还支持另外8种服务暴露协议:RMI协议.Hessian协议.HTTP协议.WebService协议.Thrift协议.Memcached协议. ...