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 0c74cad

Browse files
committed
Time: 40 ms (44.25%) | Memory: 16.6 MB (55.25%) - LeetSync
1 parent 5753599 commit 0c74cad

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
class Solution:
2+
def myAtoi(self, s: str) -> int:
3+
# clean
4+
s= s.lstrip()
5+
sign = 1
6+
7+
# read sign
8+
if s and s[0]== '-':
9+
sign = -1
10+
s=s[1:]
11+
elif s and s[0] == '+':
12+
13+
s = s[1:]
14+
15+
print(s)
16+
index = 0
17+
18+
19+
# read int
20+
for i in range(len(s)):
21+
if not s[i].isdigit():
22+
index = i
23+
break
24+
index = len(s)
25+
n = s[:index]
26+
27+
if n:
28+
n = int(n) * sign
29+
if (n > (2**31)-1):
30+
n = (2**31) -1
31+
32+
# print("if")
33+
elif (n < -(2 ** 31)):
34+
n = -(2 ** 31)
35+
# print("else")
36+
return n
37+
38+
return 0

0 commit comments

Comments
(0)

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