Skip to content

Commit 35f03ca

Browse files
committed
二刷415
1 parent a8b5a48 commit 35f03ca

File tree

3 files changed

+89
-16
lines changed

3 files changed

+89
-16
lines changed

docs/0415-add-strings.adoc

Lines changed: 43 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,41 @@
11
[#0415-add-strings]
2-
= 415. Add Strings
2+
= 415. 字符串相加
33

4-
{leetcode}/problems/add-strings/[LeetCode - Add Strings^]
4+
https://leetcode.cn/problems/add-strings/[LeetCode - 415. 字符串相加 ^]
55

6-
Given two non-negative integers `num1` and `num2` represented as string, return the sum of `num1` and `num2`.
6+
给定两个字符串形式的非负整数 `num1` `num2` ,计算它们的和并同样以字符串形式返回。
77

8-
*Note:*
8+
你不能使用任何內建的用于处理大整数的库(比如 `BigInteger`), 也不能直接将输入的字符串转换为整数形式。
99

10-
. The length of both `num1` and `num2` is < 5100.
11-
. Both `num1` and `num2` contains only digits `0-9`.
12-
. Both `num1` and `num2` does not contain any leading zero.
13-
. You *must not use any built-in BigInteger library* or *convert the inputs to integer* directly.
10+
*示例 1:*
1411

12+
....
13+
输入:num1 = "11", num2 = "123"
14+
输出:"134"
15+
....
1516

17+
*示例 2:*
18+
19+
....
20+
输入:num1 = "456", num2 = "77"
21+
输出:"533"
22+
....
23+
24+
*示例 3:*
25+
26+
....
27+
输入:num1 = "0", num2 = "0"
28+
输出:"0"
29+
....
30+
31+
*提示:*
32+
33+
* `1 \<= num1.length, num2.length \<= 10^4^`
34+
* `num1``num2` 都只包含数字 `0-9`
35+
* `num1``num2` 都不包含任何前导零
36+
37+
38+
== 思路分析
1639

1740

1841
[[src-0415]]
@@ -27,13 +50,17 @@ include::{sourcedir}/_0415_AddStrings.java[tag=answer]
2750
----
2851
--
2952
30-
// 二刷::
31-
// +
32-
// --
33-
// [{java_src_attr}]
34-
// ----
35-
// include::{sourcedir}/_0415_AddStrings_2.java[tag=answer]
36-
// ----
37-
// --
53+
二刷::
54+
+
55+
--
56+
[{java_src_attr}]
57+
----
58+
include::{sourcedir}/_0415_AddStrings_2.java[tag=answer]
59+
----
60+
--
3861
====
3962

63+
64+
== 参考资料
65+
66+

logbook/202503.adoc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,11 @@ endif::[]
240240
|{doc_base_url}/0054-spiral-matrix.adoc[题解]
241241
|✅ 使用递归来推进层级。注意处理细节。另外,在“上”和“右”能覆盖“全部”(比如一行多列或者多行一列)时,才能在“下”之前根据长度返回。尝试了每段“读取”只剩一个的方案,不行,有很多意外情况要处理。
242242

243+
|{counter:codes2503}
244+
|{leetcode_base_url}/add-strings/[415. 字符串相加^]
245+
|{doc_base_url}/0415-add-strings.adoc[题解]
246+
|✅ 双指针
247+
243248
|===
244249
˚
245250
截止目前,本轮练习一共完成 {codes2503} 道题。
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.diguage.algo.leetcode;
2+
3+
public class _0415_AddStrings_2 {
4+
// tag::answer[]
5+
/**
6+
* @author D瓜哥 · https://www.diguage.com
7+
* @since 2025-04-13 21:54:00
8+
*/
9+
public String addStrings(String num1, String num2) {
10+
int m = num1.length() - 1, n = num2.length() - 1;
11+
char[] mc = num1.toCharArray();
12+
char[] nc = num2.toCharArray();
13+
StringBuilder sb = new StringBuilder(Math.max(m, n) + 1);
14+
int carry = 0;
15+
// 看官方答案,可以通过 m>=0 || n>=0 || carry !=0 来把三个循环合并成一个
16+
while (m >= 0 && n >= 0) {
17+
int mi = mc[m--] - '0';
18+
int ni = nc[n--] - '0';
19+
int sum = carry + mi + ni;
20+
carry = sum / 10;
21+
sb.append(sum % 10);
22+
}
23+
while (m >= 0) {
24+
int mi = mc[m--] - '0';
25+
int sum = carry + mi;
26+
carry = sum / 10;
27+
sb.append(sum % 10);
28+
}
29+
while (n >= 0) {
30+
int ni = nc[n--] - '0';
31+
int sum = carry + ni;
32+
carry = sum / 10;
33+
sb.append(sum % 10);
34+
}
35+
if (carry > 0) {
36+
sb.append(carry);
37+
}
38+
return sb.reverse().toString();
39+
}
40+
// end::answer[]
41+
}

0 commit comments

Comments
 (0)