415. Add Strings
来源:互联网 发布:电子烟推荐 知乎2017 编辑:程序博客网 时间:2024/05/16 14:02
Given two non-negative numbers num1
and num2
represented as string, return the sum of num1
and num2
.
Note:
- The length of both
num1
andnum2
is < 5100. - Both
num1
andnum2
contains only digits0-9
. - Both
num1
andnum2
does not contain any leading zero. - You must not use any built-in BigInteger library or convert the inputs to integer directly.
public class Solution { public String addStrings(String num1, String num2) { String longer = num1;String shorter = num2;if (longer.length() < shorter.length()) {longer = num2;shorter = num1;}int m = longer.length();int n = shorter.length();char l[] = longer.toCharArray();char s[] = shorter.toCharArray();char base = '0';int remainder = 0;int tmp;StringBuilder res = new StringBuilder();for (int i = n - 1; i >= 0; i--) {m--;tmp = l[m] + s[i] - 2 * base + remainder;remainder = tmp / 10;res.append(tmp % 10);}for (int i = m - 1; i >= 0; i--) {tmp = l[i] - base + remainder;remainder = tmp / 10;res.append(tmp % 10);}if (remainder != 0)res.append(remainder);return res.reverse().toString(); }}
0 0
- 415. Add Strings
- Leetcode-415. Add Strings
- 415. Add Strings
- 415. Add Strings
- 415. Add Strings
- 415. Add Strings
- LeetCode 415. Add Strings
- 415. Add Strings
- 415. Add Strings
- [LeetCode]--415. Add Strings
- LeetCode 415. Add Strings
- LeetCode 415. Add Strings
- 415. Add Strings
- LeetCode 415. Add Strings
- 415. Add Strings
- 415. Add Strings
- 415. Add Strings(unsolved)
- leetcode 415. Add Strings
- Linux-find命令
- js运用sort对json 数组进行排序
- 第三周项目2——建立顺序表的算法库
- Intel Code Challenge Final Round (Div. 1 + Div. 2, Combined)题解报告
- 洛谷 1489
- 415. Add Strings
- [LeetCode]Search for a Range
- HDU 1042:N!【BigInteger】
- 关键字-分节8(do、while、for,break 与continue,goto)
- 噢?第一次写博客耶
- Map.entry详解
- mongoose中通过-id查询的方法
- 我的第一篇博文
- java.util.hashMap如何处理冲突