File tree Expand file tree Collapse file tree 4 files changed +108
-0
lines changed
Expand file tree Collapse file tree 4 files changed +108
-0
lines changed Original file line number Diff line number Diff line change 1+ #
2+ # @lc app=leetcode.cn id=289 lang=python3
3+ #
4+ # [289] 生命游戏
5+ #
6+ # @lc code=start
7+ class Solution :
8+ def gameOfLife (self , board : List [List [int ]]) -> None :
9+ """
10+ Do not return anything, modify board in-place instead.
11+ """
12+ copy_data = [[0 ] * len (board [i ]) for i in range (len (board ))]
13+ for i in range (len (board )):
14+ for j in range (len (board [i ])):
15+ if board [i ][j ] == 1 :
16+ for m in [- 1 , 0 , 1 ]:
17+ if not (0 <= i + m < len (board )):
18+ continue
19+ for n in [- 1 , 0 , 1 ]:
20+ if m == 0 and n == 0 :
21+ continue
22+ if 0 <= j + n < len (board [i ]):
23+ copy_data [i + m ][j + n ] += 1
24+
25+ for i in range (len (board )):
26+ for j in range (len (board [i ])):
27+ if board [i ][j ] == 1 :
28+ if copy_data [i ][j ] < 2 or copy_data [i ][j ] > 3 :
29+ board [i ][j ] = 0
30+ else :
31+ board [i ][j ] = 1
32+ elif copy_data [i ][j ] == 3 :
33+ board [i ][j ] = 1
34+
35+ # @lc code=end
36+
Original file line number Diff line number Diff line change 1+ #
2+ # @lc app=leetcode.cn id=299 lang=python3
3+ #
4+ # [299] 猜数字游戏
5+ #
6+
7+ # @lc code=start
8+ class Solution :
9+ def getHint (self , secret : str , guess : str ) -> str :
10+ a = 0
11+ b = 0
12+ aa = {}
13+ bb = []
14+ for i , s in enumerate (secret ):
15+ if guess [i ] == s :
16+ a += 1
17+ else :
18+ aa [s ] = aa .get (s , 0 ) + 1
19+ bb .append (guess [i ])
20+ for g in bb :
21+ if g in aa and aa [g ] > 0 :
22+ aa [g ] -= 1
23+ b += 1
24+
25+ return f'{ a } A{ b } B'
26+ # @lc code=end
27+
Original file line number Diff line number Diff line change 1+ #
2+ # @lc app=leetcode.cn id=674 lang=python3
3+ #
4+ # [674] 最长连续递增序列
5+ #
6+
7+ # @lc code=start
8+ class Solution :
9+ def findLengthOfLCIS (self , nums : List [int ]) -> int :
10+ num_ = 1
11+ start = 1
12+ for i in range (1 , len (nums )):
13+ num1 = nums [i - 1 ]
14+ num2 = nums [i ]
15+ if num2 > num1 :
16+ start += 1
17+ else :
18+ num_ = max ([start , num_ ])
19+ start = 1
20+ return max ([start , num_ ])
21+ # @lc code=end
22+
Original file line number Diff line number Diff line change 1+ #
2+ # @lc app=leetcode.cn id=680 lang=python3
3+ #
4+ # [680] 验证回文串 II
5+ #
6+
7+ # @lc code=start
8+ class Solution :
9+
10+ def validPalindrome_ (self , s , i ):
11+ for j in range (len (s ) // 2 ):
12+ if s [j ] != s [len (s ) - 1 - j ]:
13+ if i == 0 :
14+ return self .validPalindrome_ (s [j : len (s ) - 1 - j ], 1 ) | self .validPalindrome_ (s [j + 1 : len (s ) - j ], 1 )
15+ else :
16+ return False
17+
18+ return True
19+
20+ def validPalindrome (self , s : str ) -> bool :
21+ return self .validPalindrome_ (s , 0 )
22+ # @lc code=end
23+
You can’t perform that action at this time.
0 commit comments