Skip to content

Commit 82522f6

Browse files
committed
문자로된 숫자 더하기
1 parent 83c7b61 commit 82522f6

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

AddString.java

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
public class AddString {
2+
3+
// my solution_Runtime Error
4+
public String addStrings(String num1, String num2) {
5+
return String.valueOf(Integer.parseInt(num1) + Integer.parseInt(num2));
6+
}
7+
}
8+
9+
/* best solution
10+
class Solution {
11+
public String addStrings(String num1, String num2) {
12+
StringBuilder res = new StringBuilder();
13+
14+
int carry = 0;
15+
int p1 = num1.length() - 1;
16+
int p2 = num2.length() - 1;
17+
while (p1 >= 0 || p2 >= 0) {
18+
int x1 = p1 >= 0 ? num1.charAt(p1) - '0' : 0;
19+
int x2 = p2 >= 0 ? num2.charAt(p2) - '0' : 0;
20+
int value = (x1 + x2 + carry) % 10;
21+
carry = (x1 + x2 + carry) / 10;
22+
res.append(value);
23+
p1--;
24+
p2--;
25+
}
26+
27+
if (carry != 0)
28+
res.append(carry);
29+
30+
return res.reverse().toString();
31+
}
32+
}
33+
*/

0 commit comments

Comments
 (0)