Add Strings Leetcode

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();
    }
}

逻辑清晰多了。。。= =

时间: 2024-12-29 12:16:24

Add Strings Leetcode的相关文章

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

Add Binary Leetcode java

题目: Given two binary strings, return their sum (also a binary string). For example, a = "11" b = "1" Return "100". 题解: 二进制加法都是从最低位(从右加到左).所以对两个字符串要从最后一位开始加,如果遇见长度不一的情况,就把短的字符串高位补0. 每轮计算要加上进位,最后跳出循环后要坚持进位是否为1,以便更新结果. 代码如下(from

Multiply Strings leetcode java

题目: Given two numbers represented as strings, return multiplication of the numbers as a string. Note: The numbers can be arbitrarily large and are non-negative. 题解: 题意就是给你两个字符串型的数字,给这两个数字做乘法. 如果直接转换成Integer做乘法就会溢出. 所以要一步一步来. 下面讲解引用自(http://leetcodeno

[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算法题-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

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.

Isomorphic Strings leetcode

Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the characters in s can be replaced to get t. All occurrences of a character must be replaced with another character while preserving the order of characters.

893. Groups of Special-Equivalent Strings - LeetCode

Question 893. Groups of Special-Equivalent Strings Solution 题目大意: AB两个字符串相等的条件是:A中偶数位出现的字符与B中偶数位出现的字符相同且奇数位出现的字符也相同 按上述判定相等字符串的规则求去重后字符串的个数 思路: strHash函数,传一个字符串,返回该字符串的奇数位字符和偶数位字符出现的在字母表中的分布 构造一个set来存储每个字符串的strHash后的结果 Java实现: public int numSpecialEq