We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent 5753599 commit 0c74cadCopy full SHA for 0c74cad
8-string-to-integer-atoi/string-to-integer-atoi.py
@@ -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