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。

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

  1. public class Solution {
  2. public String addStrings(String num1, String num2) {
  3. if (num1 == null || num2 == null) {
  4. return null;
  5. }
  6. int sum = 0;
  7. StringBuilder str = new StringBuilder();
  8. for (int i = num1.length() - 1, j = num2.length() - 1; i >= 0 || j >= 0; i--, j--) {
  9. int x = i < 0 ? 0 : num1.charAt(i) - '0';
  10. int y = j < 0 ? 0 : num2.charAt(j) - '0';
  11. sum = x + y;
  12.  
  13. sum = sum % 10;
  14. if (flag) {
  15. if (sum + 1 >= 10) {
  16. flag = true;
  17. str.append((sum + 1) % 10);
  18. } else {
  19. str.append(sum + 1);
  20. flag = false;
  21. }
  22. } else {
  23. str.append(sum);
  24. }
  25. if (x + y >= 10) {
  26. flag = true;
  27. }
  28.  
  29. }
  30. if (flag) {
  31. str.append(1);
  32. }
  33. return str.reverse().toString();
  34. }
  35. }

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

  1. public class Solution {
  2. public String addStrings(String num1, String num2) {
  3. if (num1 == null || num2 == null) {
  4. return null;
  5. }
  6. int carry = 0;
  7. StringBuilder str = new StringBuilder();
  8. for (int i = num1.length() - 1, j = num2.length() - 1; i >= 0 || j >= 0 || carry == 1; i--, j--) {
  9. int x = i < 0 ? 0 : num1.charAt(i) - '0';
  10. int y = j < 0 ? 0 : num2.charAt(j) - '0';
  11. str.append((x + y + carry) % 10);
  12. carry = (x + y + carry) / 10;
  13. }
  14. return str.reverse().toString();
  15. }
  16. }

逻辑清晰多了。。。= =

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. 我的android学习脚步----------- 的第一个应用

    刚刚开始学android开发,以前都是在别人调好的应用中修改JNI,现在需要自己一步步走 开发环境:Eclipse+ADT 配置不多讲了,引自:http://www.cnblogs.com/allen ...

  2. UIView你知道多少

    转载自:http://www.cnblogs.com/likwo/archive/2011/06/18/2084192.html   曾经有人这么说过,在iphone里你看到的,摸到的,都是UIVie ...

  3. 某技术大牛的帖子(android项目总结)

    1. 一般性控件需要设置onclick事件才会有点击效果(selector). 2.  <item android:drawable=”@drawable/btn_ct_green” /> ...

  4. POJ 2296 Map Labeler

    二分答案 + 2-SAT验证,判断正方形是否相交写起来有点烦,思路还是挺简单的. #include<cstdio> #include<cstring> #include< ...

  5. Python3基础 list(enumerate()) 将一个列表的每一个元素转换成 带索引值的元组

    镇场诗: 诚听如来语,顿舍世间名与利.愿做地藏徒,广演是经阎浮提. 愿尽吾所学,成就一良心博客.愿诸后来人,重现智慧清净体.-------------------------------------- ...

  6. javascript 函数 add(1)(2)(3)(4)实现无限极累加 —— 一步一步原理解析

    问题:我们有一个需求,用js 实现一个无限极累加的函数, 形如 add(1) //=> 1; add(1)(2)  //=> 2; add(1)(2)(3) //=>  6; add ...

  7. 改变cinder默认vg的方法

    在存储节点:# pvcreate /dev/sdb# vgcreate vg100gb /dev/sdb # openstack-config --set /etc/cinder/cinder.con ...

  8. 伸展二叉树树(C#)

    参考过好几篇关于将伸展树的代码,发现看不懂.看图能看懂原理.就尝试自己实现了下. 自顶向上的算法. using System; using System.Collections.Generic; us ...

  9. ZenCoding 个人理解和总结

    我的理解:ZenCoding是一个html简写的语法,可以最快速的生成html. 不少IDE应该都支持,我用的intellij idea是支持的. ZenCoding表示和CSS/JS有相通之处,比如 ...

  10. linux自动启动程序

    下面用自启动apache为例: 有两种方法可以让Apache在系统启动时自动启动   1. 在/etc/rc.d/rc.local中增加启动apache的命令,例如:/usr/local/httpd/ ...