FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
LeetCode-Solutions/Python/add-strings.py at master · tehsints/LeetCode-Solutions · GitHub
tehsints
/
LeetCode-Solutions
Public
forked from
kamyu104/LeetCode-Solutions
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
LeetCode-Solutions
/
Python
/
add-strings.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
47 lines (42 loc) · 1.16 KB
Breadcrumbs
LeetCode-Solutions
/
Python
/
add-strings.py
Copy path
File metadata and controls
47 lines (42 loc) · 1.16 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
# Time: O(n)
# Space: O(1)
class
Solution
(
object
):
def
addStrings
(
self
,
num1
,
num2
):
"""
:type num1: str
:type num2: str
:rtype: str
"""
result
=
[]
i
,
j
,
carry
=
len
(
num1
)
-
1
,
len
(
num2
)
-
1
,
0
while
i
>=
0
or
j
>=
0
or
carry
:
if
i
>=
0
:
carry
+=
ord
(
num1
[
i
])
-
ord
(
'0'
)
i
-=
1
if
j
>=
0
:
carry
+=
ord
(
num2
[
j
])
-
ord
(
'0'
)
j
-=
1
result
.
append
(
str
(
carry
%
10
))
carry
/=
10
result
.
reverse
()
return
""
.
join
(
result
)
def
addStrings2
(
self
,
num1
,
num2
):
"""
:type num1: str
:type num2: str
:rtype: str
"""
length
=
max
(
len
(
num1
),
len
(
num2
))
num1
=
num1
.
zfill
(
length
)[::
-
1
]
num2
=
num2
.
zfill
(
length
)[::
-
1
]
res
,
plus
=
''
,
0
for
index
,
num
in
enumerate
(
num1
):
tmp
=
str
(
int
(
num
)
+
int
(
num2
[
index
])
+
plus
)
res
+=
tmp
[
-
1
]
if
int
(
tmp
)
>
9
:
plus
=
1
else
:
plus
=
0
if
plus
:
res
+=
'1'
return
res
[::
-
1
]
Back
|
FazBrowse Home
|
New Git URL