update
swadhinbiswas committed 2 years ago
2cf2dbd
1 parent 3e2b57a
commit 2cf2dbd
3 files changed
+78 -1
Showing 3 changed files+78-1
▼ String to Integer (atoi).py
+72-1
| @@ -1,3 +1,74 @@ | ||
1 | 1 | class Solution: |
2 | 2 | def myAtoi(self, s: str) -> int: |
3 | - | |
3 | + s=s.strip() | |
4 | + | |
5 | + out=0 | |
6 | + if not s: | |
7 | + return 0 | |
8 | + negative=False | |
9 | + if s[0] == '-': | |
10 | + negative=True | |
11 | + elif s[0] == '+': | |
12 | + negative=False | |
13 | + elif not s[0].isnumeric(): | |
14 | + return 0 | |
15 | + else: | |
16 | + out=ord(s[0])-ord("0") | |
17 | + | |
18 | + for i in range(1,len(s)): | |
19 | + if s[i].isnumeric(): | |
20 | + out=out*10+(ord(s[i])-ord("0")) | |
21 | + else: | |
22 | + break | |
23 | + | |
24 | + | |
25 | + | |
26 | + if not negative and out>=2147483647: | |
27 | + return 2147483648 | |
28 | + | |
29 | + if out>=2147483648 and negative: | |
30 | + return -2147483648 | |
31 | + | |
32 | + | |
33 | + if not negative: | |
34 | + return out | |
35 | + else: | |
36 | + return -out | |
37 | + | |
38 | + | |
39 | + # if not s: | |
40 | + # return 0 | |
41 | + | |
42 | + # negatives = False | |
43 | + # out=0 | |
44 | + # if s[0]=='-': | |
45 | + # negatives = True | |
46 | + # elif s[0]=='+': | |
47 | + # negatives = True | |
48 | + # elif not s[0].isnumeric(): | |
49 | + # return 0 | |
50 | + # else: | |
51 | + # out = ord(s[0])-ord("0") | |
52 | + # for i in range(1,len(s)): | |
53 | + # if s[i].isnumeric(): | |
54 | + # out=out*10+(ord(s[i])-ord("0")) | |
55 | + # if not negatives and out >= 2**31: | |
56 | + # return 2**31 | |
57 | + # if negatives and out<=-2**31: | |
58 | + # return -2**31 | |
59 | + # else: | |
60 | + # break | |
61 | + | |
62 | + # if not negatives: | |
63 | + # return out | |
64 | + # else: | |
65 | + # return -out | |
66 | + | |
67 | + | |
68 | + | |
69 | +b=Solution() | |
70 | + | |
71 | +x=b.myAtoi(s="3.14159") | |
72 | +print(x) | |
73 | + | |
74 | + | |
4 | 75 | |
▼Regular Expression Matching.py
+5-0
| @@ -0,0 +1,5 @@ | ||
1 | +class Solution: | |
2 | + def isMatch(self, s: str, p: str) -> bool: | |
3 | + | |
4 | + | |
5 | + | |
0 | 6 | |
▼Reverse Integer.py
+1-0
| @@ -10,3 +10,4 @@ class Solution: | ||
10 | 10 | return x |
11 | 11 | |
12 | 12 | |
13 | +print(-2147483648) | |
13 | 14 | |