【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
num1
andnum2
is< 5100
. - Both
num1
andnum2
contains only digits0-9
. - Both
num1
andnum2
does 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算法积 ...
随机推荐
- Go知识点大纲
目录 1. 基本介绍 2. 安装及配置 3. 变量 4. 常量 5. 数据类型 5.1 numeric(数字) 5.2 string(字符串) 5.3 array(数组) 5.4 slice(切片) ...
- Linux服务器I/O性能分析-1
一.IOSTAT误区 1.1 误区-svctm Linux上的svctm是重要的I/O指标(I/O平均服务时间-单位毫秒),这个值直接反映了硬件的性能(I/O请求从SCSI层发出--->I/O完 ...
- SpringCloud微服务实战——搭建企业级开发框架(二十八):扩展MybatisPlus插件DataPermissionInterceptor实现数据权限控制
一套完整的系统权限需要支持功能权限和数据权限,前面介绍了系统通过RBAC的权限模型来实现功能的权限控制,这里我们来介绍,通过扩展Mybatis-Plus的插件DataPermissionInterce ...
- day31 协程
day31 协程 一.死锁与递归锁 所谓死锁:是指两个或者两个以上的进程或线程在执行过程中,因争夺资源而造成的一种互相等待的现象,若无外力作用,它们都将无法推进下去.此时称系统处于死锁状态或系统产 ...
- 【leetcode】598. Range Addition II
You are given an m x n matrix M initialized with all 0's and an array of operations ops, where ops[i ...
- Cx_Oracle 安装
1. 下载安装 2.把oci.ddl oraociei11.dll 放到C:\Python33\Lib\site-packages路径下
- 基本类型、引用类型NPE异常
1.null是Java中的关键字,像public.static.final.它是大小写敏感的,你不能将null写成Null或NULL,编译器将不能识别它们然后报错. 2.就像每种原始类型都有默认值一样 ...
- liunux 6.5设置网卡默认开启
编辑如下文件; vi /etc/sysconfig/network-scripts/ifcfg-eth0 把 ONBOOT=no 改为 ONBOOT=yes 好了网卡会在启动机器的时候一起启动了.
- Default arguments and virtual function
Predict the output of following C++ program. 1 #include <iostream> 2 using namespace std; 3 4 ...
- Linux磁盘与文件系统原理
这一章主要是原理性的,介绍了Linux文件系统的运作原理.涉及到很多计算机组成和操作系统的原理性知识,这部分知识很多都忘了,在这里复习下. 我们只看本章第1,2节.--------------- ...