[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

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.
You must not use any built-in BigInteger library or convert the inputs to integer directly.

public String addStrings(String num1, String num2) {
        char[] num1s = num1.toCharArray();
        char[] num2s = num2.toCharArray();
        int flag = 0, temp = 0, len1 = num1s.length - 1, len2 = num2s.length - 1;
        String result = "";
        int i = len1, j = len2;
        for (; i >= 0 && j >= 0; i--, j--) {
            temp = num1s[i] - '0' + num2s[j] - '0' + flag;
            if (temp > 9) {
                flag = 1;
                temp = temp % 10;
            } else
                flag = 0;
            result = temp + result;
        }
        while (i >= 0) {
            temp = num1s[i] - '0' + flag;
            if (temp > 9) {
                flag = 1;
                temp = temp % 10;
            } else
                flag = 0;
            result = temp + result;
            i--;
        }
        while (j >= 0) {
            temp = num2s[j] - '0' + flag;
            if (temp > 9) {
                flag = 1;
                temp = temp % 10;
            } else
                flag = 0;
            result = temp + result;
            j--;
        }
        if (flag == 1)
            result = flag + result;
        return result;
    }

看别人简便的写法,只是语言结构上简便。

public String addStrings(String num1, String num2) {
        StringBuffer sb = new StringBuffer();
        int l1 = num1.length();
        int l2 = num2.length();
        char[] c1 = num1.toCharArray();
        char[] c2 = num2.toCharArray();
        int carry = 0;
        for (int i = 0; i < l1 || i < l2; i++) {
            int a1 = i < l1 ? c1[l1 - 1 - i] - 48 : 0;
            int a2 = i < l2 ? c2[l2 - 1 - i] - 48 : 0;
            int ans = a1 + a2 + carry;
            sb.append(ans % 10);
            carry = ans / 10;
        }
        if (carry == 1) {
            sb.append("1");
        }
        return sb.reverse().toString();
    }
目录
相关文章
|
5月前
Leetcode 623. Add One Row to Tree
题目很简单,在树的第d层加一层,值为v。递归增加一层就好了。代码如下
25 0
|
7月前
|
存储 C++ Python
LeetCode刷题---Add Two Numbers(一)
LeetCode刷题---Add Two Numbers(一)
|
10月前
|
存储 算法 安全
LeetCode - #2 Add Two Numbers
我们社区从本期开始会将顾毅(Netflix 增长黑客,《iOS 面试之道》作者,ACE 职业健身教练。)的 Swift 算法题题解整理为文字版以方便大家学习与阅读。 不积跬步,无以至千里;不积小流,无以成江海,Swift社区 伴你前行。
LeetCode - #2 Add Two Numbers
|
人工智能 算法
LeetCode 1347. 制造字母异位词的最小步骤数 Minimum Number of Steps to Make Two Strings Anagram
LeetCode 1347. 制造字母异位词的最小步骤数 Minimum Number of Steps to Make Two Strings Anagram
LeetCode 415. Add Strings
给定两个字符串形式的非负整数 num1 和num2 ,计算它们的和。
65 0
LeetCode 415. Add Strings
LeetCode 258. Add Digits
给定一个非负整数 num,反复将各个位上的数字相加,直到结果为一位数。
51 0
LeetCode 258. Add Digits
LeetCode 241. Different Ways to Add Parentheses
给定一个含有数字和运算符的字符串,为表达式添加括号,改变其运算优先级以求出不同的结果。你需要给出所有可能的组合的结果。有效的运算符号包含 +, - 以及 * 。
53 0
LeetCode 241. Different Ways to Add Parentheses
LeetCode 205. Isomorphic Strings
给定两个字符串 s 和 t,判断它们是否是同构的。 如果 s 中的字符可以被替换得到 t ,那么这两个字符串是同构的。 所有出现的字符都必须用另一个字符替换,同时保留字符的顺序。两个字符不能映射到同一个字符上,但字符可以映射自己本身。
54 0
LeetCode 205. Isomorphic Strings
LeetCode 67. Add Binary
给定两个二进制字符串,返回它们的总和(也是二进制字符串)。 输入字符串都是非空的,只包含字符1或0。
53 0
LeetCode 67. Add Binary
LeetCode 43. Multiply Strings
给定两个表示为字符串形式的非负整数num1和num2,返回num1和num2的乘积,也表示为字符串形式。
50 0
LeetCode 43. Multiply Strings

热门文章

最新文章