We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 310a666 commit f2eb7f8Copy full SHA for f2eb7f8
leetcode_Java/DoneTitle.txt
@@ -77,6 +77,7 @@
77
392. 判断子序列
78
402. 移掉 K 位数字
79
406. 根据身高重建队列
80
+415. 字符串相加
81
416. 分割等和子集
82
437. 路径总和
83
448. 找到所有数组中消失的数字
leetcode_Java/Solution0415.java
@@ -0,0 +1,22 @@
1
+// 415. 字符串相加
2
+
3
4
+/*
5
+模拟竖式加法,从最低位开始两数相加,和超过10则向高位进一位,其中一个数字遍历结束了则需要补0
6
+ */
7
+class Solution {
8
+ public String addStrings(String num1, String num2) {
9
+ int i = num1.length() - 1;
10
+ int j = num2.length() - 1;
11
+ int carry = 0;
12
+ StringBuilder res = new StringBuilder();
13
+ while (i >= 0 || j >= 0 || carry > 0) {
14
+ int x = i >= 0 ? num1.charAt(i--) - '0' : 0;
15
+ int y = j >= 0 ? num2.charAt(j--) - '0' : 0;
16
+ int sum = x + y + carry;
17
+ carry = sum / 10;
18
+ res.append(sum % 10);
19
+ }
20
+ return res.reverse().toString();
21
22
+}
0 commit comments