update

swadhinbiswas committed 2 years ago
3e2b57a
1 parent 69e3995
commit 3e2b57a
2 files changed
+13 -44
▼ String to Integer (atoi).py
+1-27
1
1
class Solution:
2
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
3
+
30
4
▼Zigzag Conversion.py
+12-17
1
1
class Solution:
2
2
def convert(self, s: str, numRows: int) -> str:
3
- if numRows == 1:
3
+ if numRows==1:
4
4
return s
5
- res=""
6
- for r in range(numRows):
7
- increment = 2*(numRows-1)
8
- for i in range(r,len(s),increment):
9
- res+=s[i]
10
- if r!=0 and r!=numRows-1 and i+increment-2*r<len(s):
11
- res+=s[i+increment-2*r]
12
- if (r>0 and r<numRows-1 and i+increment-2*r<len(s)):
13
- res+=s[i+increment-2*r]
14
-
15
- return res
16
-
5
+ listy=[""]*numRows
6
+ index=0
7
+ step=1
8
+ for i in s:
9
+ listy[index]+=i
10
+ if index==0:
11
+ step=1
12
+ elif index==numRows-1:
13
+ step=-1
14
+ index+=step
15
+ return "".join(listy)
17
16
18
-
19
-
20
-c=Solution()
21
-print(c.convert("PAYPALISHIRING",3)) #PAHNAPLSIIGYIR
22
17