Skip to content

Commit cd0b832

Browse files
committed
solve problem Add Strings
1 parent 5ec3c57 commit cd0b832

File tree

5 files changed

+70
-0
lines changed

5 files changed

+70
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ All solutions will be accepted!
127127
|783|[Minimum Distance Between Bst Nodes](https://leetcode-cn.com/problems/minimum-distance-between-bst-nodes/description/)|[java/py/js](./algorithms/MinimumDistanceBetweenBstNodes)|Easy|
128128
|747|[Largest Number At Least Twice Of Others](https://leetcode-cn.com/problems/largest-number-at-least-twice-of-others/description/)|[java/py/js](./algorithms/LargestNumberAtLeastTwiceOfOthers)|Easy|
129129
|455|[assign cookies](https://leetcode-cn.com/problems/assign-cookies/description/)|[java/py/js](./algorithms/AssignCookies)|Easy|
130+
|415|[Add Strings](https://leetcode-cn.com/problems/add-strings/description/)|[java/py/js](./algorithms/AddStrings)|Easy|
130131

131132
# Database
132133
|#|Title|Solution|Difficulty|

algorithms/AddStrings/README.md

Whitespace-only changes.

algorithms/AddStrings/Solution.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Solution {
2+
public String addStrings(String num1, String num2) {
3+
String res = "";
4+
int carry = 0,
5+
i = num1.length() - 1,
6+
j = num2.length() - 1;
7+
8+
while (i >= 0 || j >= 0) {
9+
int a = i >= 0 ? num1.charAt(i) - '0' : 0,
10+
b = j >= 0 ? num2.charAt(j) - '0' : 0,
11+
sum = a + b + carry;
12+
13+
res = String.valueOf(sum % 10) + res;
14+
carry = sum / 10;
15+
i--;
16+
j--;
17+
}
18+
19+
return carry == 1 ? "1" + res : res;
20+
}
21+
}

algorithms/AddStrings/solution.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/**
2+
* @param {string} num1
3+
* @param {string} num2
4+
* @return {string}
5+
*/
6+
var addStrings = function(num1, num2) {
7+
let res = '',
8+
carry = 0,
9+
i = num1.length - 1,
10+
j = num2.length - 1
11+
12+
while (i >= 0 || j >= 0) {
13+
let a = i >= 0 ? num1[i] - '0' : 0,
14+
b = j >= 0 ? num2[j] - '0' : 0,
15+
sum = a + b + carry
16+
17+
res = String(sum % 10) + res
18+
carry = parseInt(sum / 10)
19+
i--
20+
j--
21+
}
22+
23+
return carry ? '1' + res : res
24+
};

algorithms/AddStrings/solution.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
class Solution(object):
2+
def addStrings(self, num1, num2):
3+
"""
4+
:type num1: str
5+
:type num2: str
6+
:rtype: str
7+
"""
8+
res = ''
9+
carry = False
10+
i = len(num1) - 1
11+
j = len(num2) - 1
12+
13+
while i >= 0 or j >= 0:
14+
a = ord(num1[i]) - ord('0') if i >= 0 else 0
15+
b = ord(num2[j]) - ord('0') if j >= 0 else 0
16+
sm = a + b + 1 if carry else a + b
17+
18+
res = str(sm % 10) + res
19+
carry = sm >= 10
20+
21+
i -= 1
22+
j -= 1
23+
24+
return '1' + res if carry else res

0 commit comments

Comments
 (0)