solved
swadhinbiswas committed 2 years ago
84d6f77
1 parent 9fc6fe8
commit 84d6f77
2 files changed
+33 -0
Showing 2 changed files+33-0
▼Reverse Integer.py
+12-0
| @@ -0,0 +1,12 @@ | ||
1 | +class Solution: | |
2 | + def reverse(self, x: int) -> int: | |
3 | + while True: | |
4 | + if x<0: | |
5 | + x = int(str(x)[1:][::-1]) * -1 | |
6 | + else: | |
7 | + x = int(str(x)[::-1]) | |
8 | + if x < -2**31 or x > 2**31-1: | |
9 | + return 0 | |
10 | + return x | |
11 | + | |
12 | + | |
0 | 13 | |
▼Zigzag Conversion.py
+21-0
| @@ -0,0 +1,21 @@ | ||
1 | +class Solution: | |
2 | + def convert(self, s: str, numRows: int) -> str: | |
3 | + if numRows == 1: | |
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 | + | |
17 | + | |
18 | + | |
19 | + | |
20 | +c=Solution() | |
21 | +print(c.convert("PAYPALISHIRING",3)) #PAHNAPLSIIGYIR | |
0 | 22 | |