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. 国内apk加固的破解方法

    国内apk加固的破解方法 By Bob Pan 国内的apk加固技术都使用了将原有的dex隐藏, 在运行时解压, 并且通过修改app的类加载器的方式实现加固. 参考: AndoridAPK反逆向解决方 ...

  2. Mahout分布式运行实例:基于矩阵分解的协同过滤评分系统(一个命令实现文件格式的转换)

     Apr 08, 2014  Categories in tutorial tagged with Mahout hadoop 协同过滤  Joe Jiang 前言:之前配置Mahout时测试过一个简 ...

  3. BZOJ 3110:[Zjoi2013]K大数查询(整体二分)

    http://www.lydsy.com/JudgeOnline/problem.php?id=3110 题意:-- 思路:其实和之前POJ那道题差不多,只不过是换成区间更新,而且是第k大不是第k小, ...

  4. decoder3_8

    这两天回归书本,继续阅读书上的内容,此时的体会与刚开始学那会的体会是不一样的,比如3_8decoder,之前就认为可以用case来写,而书上有一种更简便的方式来描述,带给你新的思路,既然有新方式可以描 ...

  5. 分享我们项目中基于EF事务机制的架构 【转载】

    http://www.cnblogs.com/leotsai/p/how-to-use-entity-framework-transaction-scope.html 写在前面: 1. 本文中单元测试 ...

  6. android缓存之Lrucache 和LinkedHashMap

    两者的区别 网上有很多人使用软引用加载图片的多 ,但是现在已经不再推荐使用这种方式了,(1)因为从 Android 2.3 (API Level 9)开始,垃圾回收器会更倾向于回收持有软引用或弱引用的 ...

  7. CSS实现三角形方法二--border+content

    方法说明: 1.将一个div块的内容设置为空(content=" "), 2.设置它的边框(上下左右)颜色为透明(transparent), 3.设置它的左侧边框颜色为pink. ...

  8. CSS实现三角形方法一--rotate+relative

    方法说明:两个正方形,一个小的,一个大的,将大的正方向进行旋转,然后移动到小的正方形的合适位置,覆盖小正方形的一部分,使小正方形剩余部分为三角形,再把大正方形的背景色改为浏览器窗口的颜色. 用到知识: ...

  9. frame间跳转及相关问题

    body{ font-family: "Microsoft YaHei UI","Microsoft YaHei",SimSun,"Segoe UI& ...

  10. Cantor表 1083

    题目描述 Description 现代数学的著名证明之一是Georg Cantor证明了有理数是可枚举的.他是用下面这一张表来证明这一命题的: 1/1 1/2 1/3 1/4 1/5 - 2/1 2/ ...