Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 6ec6dc2

Browse files
authored
Update Day-9_Add_Strings.py
1 parent 5c3fbb1 commit 6ec6dc2

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

‎Leetcode_30day_challenge/August_Challenge_2021/Day-9_Add_Strings.py‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,29 @@ def addStrings(self, num1: str, num2: str) -> str:
2121
ans = str(carry) + ans
2222

2323
return ans
24+
25+
26+
# Using Dictionary - Not using int() anywhere in the code.
27+
class Solution:
28+
def addStrings(self, num1: str, num2: str) -> str:
29+
d = {'0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9}
30+
n = max(len(num1), len(num2))
31+
num1 = num1.rjust(n, '0')
32+
num2 = num2.rjust(n, '0')
33+
ans = ''
34+
carry = 0
35+
for i in range(len(num1)-1,-1,-1):
36+
temp = d[num1[i]] + d[num2[i]] + carry
37+
#print(temp)
38+
if temp > 9:
39+
carry = 1
40+
41+
else:
42+
carry = 0
43+
44+
ans = str(temp)[-1] + ans
45+
if carry > 0:
46+
ans = str(carry) + ans
47+
48+
return ans
49+

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /