作者: 负雪明烛
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:

  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.

题目大意

不使用大整数的情况下,计算两个字符串表示的数字的和。

解题方法

我的想法出奇的直白,就是模仿小学学的两个数的相加,从末尾开始向前以次相加,注意进位,两个数的相加最多只能进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

注意这几点:

  1. 两个数的位数保持一致才可。
  2. 加法的顺序是从个位向前
  3. 提前计算好两个数字的位数的差,否则在相加的时候会有变化。

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)的更多相关文章

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

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

  2. 36. leetcode 415. Add Strings

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

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

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

  4. LeetCode 258 Add Digits 解题报告

    题目要求 Given a non-negative integer num, repeatedly add all its digits until the result has only one d ...

  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 integers num1 and num2 represented as string, return the sum of num1 and num2 ...

  7. 【LeetCode】120. Triangle 解题报告(Python)

    [LeetCode]120. Triangle 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址htt ...

  8. 【leetcode】415. Add Strings

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

  9. LeetCode 1 Two Sum 解题报告

    LeetCode 1 Two Sum 解题报告 偶然间听见leetcode这个平台,这里面题量也不是很多200多题,打算平时有空在研究生期间就刷完,跟跟多的练习算法的人进行交流思想,一定的ACM算法积 ...

随机推荐

  1. Python基础之流程控制for循环

    目录 1. 语法 2. for+break 3. for+continue 4. for循环嵌套 1. 语法 while循环可以对任何内容循环,但循环次数不可控 for循环基于容器类型的长度,循环次数 ...

  2. oracle 将电话号码中间4位数以星号*代替

    select replace('17665312355',substr('17665312355',4,4),'****')  as phone,                       #类似E ...

  3. 8.7 进程间的通讯:管道、消息队列、共享内存、信号量、信号、Socket

    进程间的通讯 进程间为什么需要通讯? 共享数据.数据传输.消息通知.进程控制 进程间的通讯有哪些类型? 首先,联系前面讲过的知识,进程之间的用户地址空间是相互独立的,不能进行互相访问,但是,内核空间却 ...

  4. C语言中不用 + 和 - 求两个数之和

    (二)解题 题目大意:不用+或者-实现两个整数的加法 解题思路:不用+或者-,就自然想到位运算,无非就是与或非来实现二进制的加法 首先,我们来看一位二进制的加法和异或运算 A B A&B A^ ...

  5. A Child's History of England.10

    In the next reign, which was the reign of Edward, surnamed The Elder, who was chosen in council to s ...

  6. 容器之分类与各种测试(三)——slist的用法

    slist和forward_list的不同之处在于其所在的库 使用slist需要包含 #include<ext\list> 而使用forward_list则需要包含 #include< ...

  7. 【STM32】使用SDIO进行SD卡读写,包含文件管理FatFs(五)-文件管理初步介绍

    其他链接 [STM32]使用SDIO进行SD卡读写,包含文件管理FatFs(一)-初步认识SD卡 [STM32]使用SDIO进行SD卡读写,包含文件管理FatFs(二)-了解SD总线,命令的相关介绍 ...

  8. Java中特殊的类——包装类

    Java中特殊的类--包装类 包装类就是将基本数据类型封装在类中. 1.包装类 (1)自定义包装类 将基本数据类型包装成一个类对象的本质就是使用Object进行接收处理. 此时IntDemo类就是in ...

  9. Linux学习 - 目录表

    目录名 作用 权限 说明 /bin/ 存放系统命令的目录 所有用户 存放在/bin/下的命令单用户模式下也可以执行 /sbin/ 保存和系统环境设置相关的命令 root                 ...

  10. 搭建内网Yum源

    搭建内网yum源 阅读(2,238) 一:因内网服务器 众多,当统一安装一些比较大的rpm的时候全部从外网下载就比较慢,而且还占用了一定的出口流量,因此在内网部署了一台yum服务器,将阿里云的epel ...