Skip to content

Commit ecf56eb

Browse files
Add files via upload
1 parent 7b641e9 commit ecf56eb

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

Add Strings/Add_Strings.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# 基本思路,按位相加
2+
# 60ms 67.76%
3+
class Solution:
4+
def addStrings(self, num1, num2):
5+
"""
6+
:type num1: str
7+
:type num2: str
8+
:rtype: str
9+
"""
10+
res_list, carry = [], 0
11+
num1, num2 = list(num1), list(num2)
12+
while num1 or num2:
13+
n1 = ord(num1.pop()) - ord('0') if num1 else 0
14+
n2 = ord(num2.pop()) - ord('0') if num2 else 0
15+
16+
temp = n1 + n2 + carry
17+
res_list.append(str(temp % 10))
18+
carry = temp // 10
19+
20+
if carry is not 0:
21+
res_list.append(str(carry))
22+
return ''.join(res_list[::-1])

0 commit comments

Comments
 (0)