Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 662ee2e

Browse files
committed
415_Add_Strings
1 parent cd1cb10 commit 662ee2e

File tree

3 files changed

+69
-0
lines changed

3 files changed

+69
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ Also, there are open source implementations for basic data structs and algorithm
131131
| 408 | [Valid Word Abbreviation](https://leetcode.com/problems/valid-word-abbreviation/) ♥ | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/408_Valid_Word_Abbreviation.py) | Go over abbr and word, O(n) and O(1) |
132132
| 409 | [Longest Palindrome](https://leetcode.com/problems/longest-palindrome/description/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/409_Longest_Palindrome.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/409_Longest_Palindrome.java) | Length of Palindrome is always 2n or 2n + 1. So, get all possible 2*n, and choose a single one as 1 if it exists. |
133133
| 412 | [Fizz Buzz](https://leetcode.com/problems/fizz-buzz/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/412_Fizz_Buzz.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/412_Fizz_Buzz.java) | 1. From 1 to n, condition check<br>2. Condition check and string connect |
134+
| 415 | [Add Strings](https://leetcode.com/problems/add-strings/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/415_Add_Strings.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/415_Add_Strings.java) | Two points, careful abour carry, O(n) and O(n) |
134135
| 416 | [Partition Equal Subset Sum](https://leetcode.com/problems/partition-equal-subset-sum/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/416_Partition_Equal_Subset_Sum.py) | DP, Check if sum of some elements can be half of total sum, O(total_sum / 2 * n) and O(total_sum / 2) |
135136
| 421 | [Maximum XOR of Two Numbers in an Array](https://leetcode.com/problems/maximum-xor-of-two-numbers-in-an-array/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/421_Maximum_XOR_of_Two_Numbers_in_an_Array.py) | Check 0~32 prefix, check if there is x y in prefixes, where x ^ y = answer ^ 1, O(32n) and O(n) |
136137
| 422 | [Valid Word Square](https://leetcode.com/problems/valid-word-square/) &hearts; | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/422_Valid_Word_Square.py) | Compare row with column, O(n^2) |

java/415_Add_Strings.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
public class Solution {
2+
public String addStrings(String num1, String num2) {
3+
StringBuilder sb = new StringBuilder();
4+
int carry = 0;
5+
// condition is great
6+
// https://leetcode.com/problems/add-strings/discuss/90436/Straightforward-Java-8-main-lines-25ms
7+
for(int i = num1.length() - 1, j = num2.length() - 1; i >= 0 || j >= 0 || carry == 1; i--, j--){
8+
int x = i < 0 ? 0 : num1.charAt(i) - '0';
9+
int y = j < 0 ? 0 : num2.charAt(j) - '0';
10+
sb.append((x + y + carry) % 10);
11+
carry = (x + y + carry) / 10;
12+
}
13+
return sb.reverse().toString();
14+
}
15+
}

python/415_Add_Strings.py

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
class Solution(object):
2+
# def addStrings(self, num1, num2):
3+
# """
4+
# :type num1: str
5+
# :type num2: str
6+
# :rtype: str
7+
# """
8+
# if num1 is None:
9+
# num1 = '0'
10+
# if num2 is None:
11+
# num2 = '0'
12+
# res = []
13+
# carry = 0
14+
# ls = min(len(num1), len(num2))
15+
# pos = -1
16+
# while pos + ls >= 0:
17+
# curr = int(num1[pos]) + int(num2[pos]) + carry
18+
# res.insert(0, str(curr % 10))
19+
# carry = curr / 10
20+
# pos -= 1
21+
# while pos + len(num1) >= 0:
22+
# curr = int(num1[pos]) + carry
23+
# res.insert(0, str(curr % 10))
24+
# carry = curr / 10
25+
# pos -= 1
26+
# while pos + len(num2) >= 0:
27+
# curr = int(num2[pos]) + carry
28+
# res.insert(0, str(curr % 10))
29+
# carry = curr / 10
30+
# pos -= 1
31+
# if carry != 0:
32+
# res.insert(0, str(carry))
33+
# return ''.join(res)
34+
35+
def addStrings(self, num1, num2):
36+
res = []
37+
pos1 = len(num1) - 1
38+
pos2 = len(num2) - 1
39+
carry = 0
40+
# This conditon is great
41+
# https://leetcode.com/problems/add-strings/discuss/90436/Straightforward-Java-8-main-lines-25ms
42+
while pos1 >= 0 or pos2 >= 0 or carry == 1:
43+
digit1 = digit2 = 0
44+
if pos1 >= 0:
45+
digit1 = ord(num1[pos1]) - ord('0')
46+
if pos2 >= 0:
47+
digit2 = ord(num2[pos2]) - ord('0')
48+
res.append(str((digit1 + digit2 + carry) % 10))
49+
carry = (digit1 + digit2 + carry) / 10
50+
pos1 -= 1
51+
pos2 -= 1
52+
# reverse res
53+
return ''.join(res[::-1])

0 commit comments

Comments
 (0)