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.

这道题不能用long会越界,所以只能用string直接来append。精髓在于设置一个变量来储存进位。

自己先写了一个用boolean型flag来储存进位,结果代码逻辑麻烦极了。。。要考虑到两个数字相加等于9,再加上进位的1等于10的情况,还要考虑到两个数相加,和的长度长于任何一个数的长度的时候,最高位也要加上1。

这是我一开始写的代码,自己都看不下去了。。。

public class Solution {
public String addStrings(String num1, String num2) {
if (num1 == null || num2 == null) {
return null;
}
int sum = 0;
StringBuilder str = new StringBuilder();
for (int i = num1.length() - 1, j = num2.length() - 1; i >= 0 || j >= 0; i--, j--) {
int x = i < 0 ? 0 : num1.charAt(i) - '0';
int y = j < 0 ? 0 : num2.charAt(j) - '0';
sum = x + y; sum = sum % 10;
if (flag) {
if (sum + 1 >= 10) {
flag = true;
str.append((sum + 1) % 10);
} else {
str.append(sum + 1);
flag = false;
}
} else {
str.append(sum);
}
if (x + y >= 10) {
flag = true;
} }
if (flag) {
str.append(1);
}
return str.reverse().toString();
}
}

但其实有更简洁的办法,用一个int型代表最高位,当它是1的时候继续循环append。

public class Solution {
public String addStrings(String num1, String num2) {
if (num1 == null || num2 == null) {
return null;
}
int carry = 0;
StringBuilder str = new StringBuilder();
for (int i = num1.length() - 1, j = num2.length() - 1; i >= 0 || j >= 0 || carry == 1; i--, j--) {
int x = i < 0 ? 0 : num1.charAt(i) - '0';
int y = j < 0 ? 0 : num2.charAt(j) - '0';
str.append((x + y + carry) % 10);
carry = (x + y + carry) / 10;
}
return str.reverse().toString();
}
}

逻辑清晰多了。。。= =

Add Strings Leetcode的更多相关文章

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

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

  2. LeetCode——Add Strings

    LeetCode--Add Strings Question Given two non-negative integers num1 and num2 represented as string, ...

  3. 36. leetcode 415. Add Strings

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

  4. 【leetcode】415. Add Strings

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

  5. LeetCode_415. Add Strings

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

  6. 447. Add Strings

    原文题目: 447. Add Strings 解题: 字符串的当做整数来做加法,其实就是大数加法的简化版本 思路: 1)考虑不同位数,如"1234"+“45”,需要先处理低两位,再 ...

  7. [LeetCode] Add Strings 字符串相加

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

  8. LeetCode Add Strings

    原题链接在这里:https://leetcode.com/problems/add-strings/ 题目: Given two non-negative numbers num1 and num2  ...

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

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

随机推荐

  1. 用memcached的时候找key找不到,写了个命令来找找

    for i in $(seq 30); do echo "stats cachedump $i 0" | nc 192.168.88.150 11211 | grep groupS ...

  2. zf-关于注册码过期

    Webroot-index.jsp下 少写了个函数 导致登陆进去不能弹出注册码过期的对话框,函数如下 window.onload = function() { <ww:iterator valu ...

  3. POJ 1426 Find The Multiple BFS

    没什么好说的 从1开始进行广搜,因为只能包涵0和1,所以下一次需要搜索的值为next=now*10 和 next=now*10+1,每次判断一下就可以了,但是我一直不太明白我的代码为什么C++提交会错 ...

  4. Java——异常谜题

    1.谜题36  try和finally语句,代码如下,判断输出 public class Indecisive { public static void main(String[] args) { S ...

  5. CSS Font-family常用设置

    font-family: "Avenir Next", Avenir, "Helvetica Neue", Helvetica, "Lantinghe ...

  6. gulp用法

    转载:http://segmentfault.com/blog/laopopo/1190000000372547 http://javascript.ruanyifeng.com/tool/gulp. ...

  7. (一)phoneGap之环境搭建教程及其example分析

    phoneGap之环境搭建教程及其example分析 一.环境搭建 与普通的开发android应用一样,phoneGap也同于原生android应用一样,环境相同,只是有部分不同,下面就我做理解,进行 ...

  8. 前端设计——js实现图片切换的淡入淡出

    1.网页结构如图所示 2.页面布局设计细节 ①分块:一个小方块是一个div. ②无序列表一般是竖直排放的,可以通过float让其水平排放.float:left; ③三个小方块是浮动上去的,所以要用到p ...

  9. Redis数据备份和重启恢复

    一.对Redis持久化的探讨与理解 目前Redis持久化的方式有两种: RDB 和 AOF 首先,我们应该明确持久化的数据有什么用,答案是用于重启后的数据恢复. Redis是一个内存数据库,无论是RD ...

  10. Quartz.NET总结(一)

    Quartz.NET总结(一) 前段时间,花了大量的时间,将原先的计划任务,切换到Quartz.NET来进行管理.原先的后台定时服务都是通过计划任务来实现的,但是随着业务增长,计划任务也越来越多,每个 ...