File tree 2 files changed +42
-0
lines changed
0000-0099/0005.Longest Palindromic Substring
0100-0199/0118.Pascal's Triangle
2 files changed +42
-0
lines changed Original file line number Diff line number Diff line change
1
+ class Solution :
2
+ def longestPalindrome (self , s : str ) -> str :
3
+ dp = [[0 for _ in range (len (s ) + 1 )] for _ in range (len (s ) + 1 )]
4
+
5
+ for i in range (len (s )):
6
+ dp [i ][i ] = 1
7
+
8
+ for i in range (len (s ) - 1 , - 1 , - 1 ):
9
+ for j in range (i + 1 , len (s )):
10
+ if s [i ] == s [j ]:
11
+ if i + 1 == j :
12
+ dp [i ][j ] = 2
13
+ elif i + 2 == j :
14
+ dp [i ][j ] = 3
15
+ else :
16
+ if dp [i + 1 ][j - 1 ] != 0 :
17
+ dp [i ][j ] = 2 + dp [i + 1 ][j - 1 ]
18
+ maximum = max (map (max , dp ))
19
+ if maximum == 0 :
20
+ return ""
21
+ for i in range (len (s )):
22
+ for j in range (len (s )):
23
+ if dp [i ][j ] == maximum :
24
+ return s [i : j + 1 ]
Original file line number Diff line number Diff line change
1
+ class Solution :
2
+ def generate (self , numRows : int ) -> List [List [int ]]:
3
+ res = [[1 ], [1 , 1 ]]
4
+ if numRows == 0 :
5
+ return []
6
+ elif numRows == 1 :
7
+ return [res [0 ]]
8
+
9
+ # from the 3. row on
10
+ for i in range (2 , numRows ):
11
+ counter = 1
12
+ temp = [1 , 1 ]
13
+ # should add len(res[i - 1]) - 1 elements in to temp
14
+ for j in range (len (res [i - 1 ]) - 1 ):
15
+ temp .insert (counter , res [i - 1 ][j ] + res [i - 1 ][j + 1 ])
16
+ counter += 1
17
+ res .append (temp )
18
+ return res
You can’t perform that action at this time.
0 commit comments