# 31 看不懂题的看一看leetcode官方的解析就行了. 我也是看了半天才懂的
class Solution:
def nextPermutation(self, nums: List[int]) -> None:
"""
Do not return anything, modify nums in-place instead.
"""
i = len(nums) - 2
while i >= 0 and nums[i] >= nums[i + 1]:
i -= 1
if i >= 0:
j = len(nums) - 1
while j >= 0 and nums[i] >= nums[j]:
j -= 1
nums[i] , nums[j] = nums[j] , nums[i]
l , r = i + 1 , len(nums) - 1
while l < r:
nums[l] , nums[r] = nums[r] , nums[l]
l += 1
r -= 1
# 32
class Solution:
def longestValidParentheses(self, s: str) -> int:
stack = [-1]
res = 0
for i , char in enumerate(s):
if char == '(':
stack.append(i)
else:
stack.pop()
if stack:
res = max(res , i - stack[-1])
else:
stack.append(i)
return res
# 33 偷懒 and 二分
class Solution:
def search(self, nums: List[int], target: int) -> int:
return nums.index(target) if target in nums else -1
class Solution:
def search(self, nums: List[int], target: int) -> int:
if len(nums) == 0 or target not in nums:
return -1
l , r = 0 , len(nums) - 1
while l <= r:
mid = l + (r - l) // 2
if nums[mid] == target:
return mid
elif nums[mid] >= nums[0]:
if nums[mid] < target or target < nums[0]:
l = mid + 1
else:
r = mid - 1
else:
if nums[mid] < target and target < nums[0]:
l = mid + 1
else:
r = mid - 1
# 34 继续二分
class Solution:
def searchRange(self, nums, target):
n = len(nums)
if n == 0:
return [-1,-1]
res = [-1,-1]
l , r = 0 , n - 1
while l < r:
mid=(l + r) // 2
if nums[mid] < target:
l = mid + 1
elif nums[mid] > target:
r = mid - 1
else:
l = mid
r = mid
if nums[l] == target:
while l > 0 and nums[l - 1] == target:
l -= 1
while r < len(nums) - 1and nums[r + 1] == target:
r += 1
res = [l , r]
return res
# 35 继续二分
class Solution:
def searchInsert(self, nums: List[int], target: int) -> int:
l , r = 0 , len(nums)
while l < r:
mid = (l + r) // 2
if nums[mid] == target:
return mid
elif nums[mid] > target:
r = mid
else:
l = mid + 1
return l
# 36 36 37懒得写了 抄的官方
class Solution:
def isValidSudoku(self, board: List[List[str]]) -> bool:
rows = [{} for i in range(9)]
columns = [{} for i in range(9)]
boxes = [{} for i in range(9)]
for i in range(9):
for j in range(9):
num = board[i][j]
if num != '.':
num = int(num)
box_index = (i // 3 ) * 3 + j // 3
rows[i][num] = rows[i].get(num, 0) + 1
columns[j][num] = columns[j].get(num, 0) + 1
boxes[box_index][num] = boxes[box_index].get(num, 0) + 1
if rows[i][num] > 1 or columns[j][num] > 1 or boxes[box_index][num] > 1:
return False
return True
# 37
class Solution:
def solveSudoku(self, board: List[List[str]]) -> None:
"""
Do not return anything, modify board in-place instead.
"""
def dfs(pos: int):
nonlocal valid
if pos == len(spaces):
valid = True
return
i, j = spaces[pos]
for digit in range(9):
if line[i][digit] == column[j][digit] == block[i // 3][j // 3][digit] == False:
line[i][digit] = column[j][digit] = block[i // 3][j // 3][digit] = True
board[i][j] = str(digit + 1)
dfs(pos + 1)
line[i][digit] = column[j][digit] = block[i // 3][j // 3][digit] = False
if valid:
return
line = [[False] * 9 for _ in range(9)]
column = [[False] * 9 for _ in range(9)]
block = [[[False] * 9 for _a in range(3)] for _b in range(3)]
valid = False
spaces = list()
for i in range(9):
for j in range(9):
if board[i][j] == ".":
spaces.append((i, j))
else:
digit = int(board[i][j]) - 1
line[i][digit] = column[j][digit] = block[i // 3][j // 3][digit] = True
dfs(0)
# 38 9.46%.....就这样把...
class Solution:
def countAndSay(self, n: int) -> str:
if n == 1:
return '1'
tmp = '1'
for i in range(1 , n):
res = ''
start , end = 0 , 0
cnt = 0
while end < len(tmp):
while end < len(tmp) and tmp[end] == tmp[start]:
cnt += 1
end += 1
res += str(cnt) + tmp[start]
cnt = 0
start = end
tmp = res
return res
# 39 经典题之一
class Solution:
def combinationSum(self, s , target):
res = []
s.sort()
def dfs(remain , tmp , start):
if remain == 0:
res.append(tmp)
else:
for i , n in enumerate(s[start:]):
if remain < n:
break
dfs(remain - n , tmp + [n] , start + i)
dfs(target , [] , 0)
return res
# 40 跟39一模一样 只不过 start + i 变成 start + i + 1了(防制重复利用同一元素)
class Solution:
def combinationSum2(self, s: List[int], target: int) -> List[List[int]]:
if sum(s) < target:
return []
res = []
s.sort()
def dfs(remain , tmp , start):
if remain == 0 and tmp not in res:
res.append(tmp)
else:
for i , n in enumerate(s[start:]):
if remain < n:
break
dfs(remain - n , tmp + [n] , start + i + 1)
dfs(target , [] , 0)
return res