Add Strings

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.

代码的主要思路就是一位一位相加。具体代码实现过程如下。

public String addStrings(String num1, String num2) {
        int len1 = num1.length() - 1;
        int len2 = num2.length() - 1;
        int add = 0;
        StringBuilder s = new StringBuilder();
        String tem = "";
        if (num1.length() >= num2.length()) {

            while (len2 >= 0) {
                int x = Integer.parseInt(num1.charAt(len1) + "") +
                        Integer.parseInt(num2.charAt(len2) + "") + add;
                tem = Integer.toString(x % 10);
                s.append(tem);
                add = x / 10;
                len1--;
                len2--;
            }

            if(len1>=0) {
                while (len1 >= 0) {
                    int x = Integer.parseInt(num1.charAt(len1) + "") + add;
                    tem = Integer.toString(x % 10);
                    s.append(tem);
                    add = x / 10;
                    len1--;
                }
            }
            if(add>0) {
                s.append(add);

            }

        }else {
         return addStrings(num2,num1);
        }
        return s.reverse().toString();
    }

但是关注了一下,评论区大神的代码。

StringBuilder sb = new StringBuilder();
        int carry = 0;
        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‘;
            sb.append((x + y + carry) % 10);
            carry = (x + y + carry) / 10;
        }
        return sb.reverse().toString();

代码比我的简洁,另外在内存上也有很大程度的优化。这主要是string和stringbuffer的区别。string是字符串常量,而stringbuffer是字符串变量,简要的说,每次对string类型进行改变的时候其实等同于生成了一个新的string,仍后指针指向新的string对象,所以经常改变字符串最好不要用string因为每次生成对象都会对系统性能产生影响,特别当内存中无引用对象多了以后, JVM 的 GC 就会开始工作,那速度是一定会相当慢的。

如果是使用 StringBuffer 类则结果就不一样了,每次结果都会对 StringBuffer 对象本身进行操作,而不是生成新的对象,再改变对象引用。所以在一般情况下我们推荐使用 StringBuffer ,特别是字符串对象经常改变的情况下。

另外对z字符串转int类型可以不用integer转换,还可以对‘0‘,‘a‘,‘A’等做减法。

原文地址:https://www.cnblogs.com/liuchuan9504/p/8338079.html

时间: 2024-08-07 08:21:26

Add Strings的相关文章

36. leetcode 415. Add Strings

415. Add Strings Given two non-negative integers num1 and num2 represented as string, return the sum of num1 and num2. Note: The length of both num1 and num2 is < 5100. Both num1 and num2 contains only digits 0-9. Both num1 and num2 does not contain

[LeetCode] Add Strings 字符串相加

Given two non-negative numbers num1 and num2 represented as string, return the sum of num1 and num2. Note: The length of both num1 and num2 is < 5100. Both num1 and num2 contains only digits 0-9. Both num1 and num2 does not contain any leading zero.

【LeetCode415】Add Strings

题目描述: 解决思路: 此题较简单,和前面[LeetCode67]方法一样. Java代码: 1 public class LeetCode415 { 2 public static void main(String[] args) { 3 String a="1",b="9"; 4 System.out.println(a+"和"+b+"相加的结果是:"+new Solution().addStrings(a, b)); 5

415.两个字符串相加 Add Strings

Given two non-negative numbers num1 and num2 represented as string, return the sum of num1 and num2. Note: The length of both num1 and num2 is < 5100. Both num1 and num2 contains only digits 0-9. Both num1 and num2 does not contain any leading zero.

Add Strings Leetcode

Given two non-negative integers num1 and num2 represented as string, return the sum of num1 and num2. Note: The length of both num1 and num2 is < 5100. Both num1 and num2 contains only digits 0-9. Both num1 and num2 does not contain any leading zero.

415. Add Strings

BitInteger的最简单的部分 把两个string反过来,从后往前加,可以学习的地方是对于while的条件可以设置成,index1< len1 || index2 < len2. 然后在内部分别对index1 < len1和index2 <len2处理,这样就不用对其中一个提前结束的情况单独再写一个循环了 1 public String addStrings(String num1, String num2) { 2 if(num1 == null) { 3 return nu

Leetcode: Add Strings

Given two non-negative numbers num1 and num2 represented as string, return the sum of num1 and num2. Note: The length of both num1 and num2 is < 5100. Both num1 and num2 contains only digits 0-9. Both num1 and num2 does not contain any leading zero.

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

Given two non-negative numbers num1 and num2 represented as string, return the sum of num1 and num2. Note: The length of both num1 and num2 is < 5100. Both num1 and num2 contains only digits 0-9. Both num1 and num2 does not contain any leading zero.

LeetCode算法题-Add Strings(Java实现)

这是悦乐书的第223次更新,第236篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第90题(顺位题号是415).给定两个非负整数num1和num2表示为字符串,返回num1和num2的总和. 注意: num1和num2的长度均<5100. num1和num2都只包含数字0-9. num1和num2都不包含任何前导零. 您不能使用任何内置BigInteger库或直接将输入转换为整数. 本次解题使用的开发工具是eclipse,jdk使用的版本是1.8,环境是win7 6