GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — leetcode/python/415_Add_Strings.py at master · qiyuangong/leetcode

Expand file tree

/

415_Add_Strings.py

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
53 lines (52 loc) · 1.7 KB
/

415_Add_Strings.py

Copy path

File metadata and controls

53 lines (52 loc) · 1.7 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
class Solution(object):
# def addStrings(self, num1, num2):
# """
# :type num1: str
# :type num2: str
# :rtype: str
# """
# if num1 is None:
# num1 = '0'
# if num2 is None:
# num2 = '0'
# res = []
# carry = 0
# ls = min(len(num1), len(num2))
# pos = -1
# while pos + ls >= 0:
# curr = int(num1[pos]) + int(num2[pos]) + carry
# res.insert(0, str(curr % 10))
# carry = curr / 10
# pos -= 1
# while pos + len(num1) >= 0:
# curr = int(num1[pos]) + carry
# res.insert(0, str(curr % 10))
# carry = curr / 10
# pos -= 1
# while pos + len(num2) >= 0:
# curr = int(num2[pos]) + carry
# res.insert(0, str(curr % 10))
# carry = curr / 10
# pos -= 1
# if carry != 0:
# res.insert(0, str(carry))
# return ''.join(res)
def addStrings(self, num1, num2):
res = []
pos1 = len(num1) - 1
pos2 = len(num2) - 1
carry = 0
# This conditon is great
# https://leetcode.com/problems/add-strings/discuss/90436/Straightforward-Java-8-main-lines-25ms
while pos1 >= 0 or pos2 >= 0 or carry == 1:
digit1 = digit2 = 0
if pos1 >= 0:
digit1 = ord(num1[pos1]) - ord('0')
if pos2 >= 0:
digit2 = ord(num2[pos2]) - ord('0')
res.append(str((digit1 + digit2 + carry) % 10))
carry = (digit1 + digit2 + carry) / 10
pos1 -= 1
pos2 -= 1
# reverse res
return ''.join(res[::-1])
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.