update

swadhinbiswas committed 2 years ago
69e3995
1 parent 84d6f77
commit 69e3995
1 file changed
+29 -0
Showing 1 changed file+29-0
▼ String to Integer (atoi).py
+29-0
1
+class Solution:
2
+ def myAtoi(self, s: str) -> int:
3
+ num:int = 0
4
+
5
+ for i in range(len(s)):
6
+ sign:int = 1
7
+ if s[i]=='-':
8
+ sign = -1
9
+ elif s[i]==' ':
10
+ continue
11
+ elif s[i].isdigit():
12
+ num = num * 10 + int(s[i])
13
+ if num > 2**31-1:
14
+ return 2**31-1
15
+ if num < -2**31:
16
+ return -2**31
17
+ else:
18
+ break
19
+
20
+ return num * sign
21
+
22
+
23
+
24
+
25
+# Time Complexity: O(n)
26
+
27
+
28
+c=Solution()
29
+print(c.myAtoi("4245 fuck you")) #42
0
30