立个flag,1-100题每天分配10题,不会就先空着(2,8)。
昨天周六,摆烂一天。
python
class Solution:
def nextPermutation(self, nums: List[int]) -> None:
"""
Do not return anything, modify nums in-place instead.
"""
i = len(nums) - 1
while i > 0:
if nums[i - 1] < nums[i]:
break
else:
i = i - 1
if i == 0:
nums.sort()
else:
idx1 = i - 1
num1 = nums[idx1]
i = len(nums) - 1
while i > idx1:
if nums[i] > num1:
break
else:
i = i - 1
idx2 = i
num2 = nums[idx2]
nums[idx1], nums[idx2] = num2, num1
left = idx1 + 1
right = len(nums) - 1
while left < right:
nums[left], nums[right] = nums[right], nums[left]
left = left + 1
right = right - 1
python
class Solution:
def search(self, nums: List[int], target: int) -> int:
left = 0
right = len(nums) - 1
while left <= right:
mid = left + (right - left) // 2
if nums[mid] == target:
return mid
if nums[left] <= nums[mid]:
if nums[left] <= target < nums[mid]:
right = mid - 1
else:
left = mid + 1
elif nums[left] > nums[mid]:
if nums[mid] < target <= nums[right]:
left = mid + 1
else:
right = mid - 1
return -1
python
class Solution:
def searchRange(self, nums: List[int], target: int) -> List[int]:
def searchLeft(nums, target):
left = 0
right = len(nums) - 1
while left <= right:
mid = left + (right - left) // 2
if nums[mid] < target:
left = mid + 1
elif nums[mid] >= target:
right = mid - 1
if nums[left] == target:
return left
else:
return -1
def searchRight(nums, target):
left = 0
right = len(nums) - 1
while left <= right:
mid = left + (right - left) // 2
if nums[mid] > target:
right = mid - 1
elif nums[mid] <= target:
left = mid + 1
if nums[right] == target:
return right
else:
return -1
if len(nums) == 0 or nums[0] > target or nums[-1] < target:
return [-1, -1]
else:
left = searchLeft(nums, target)
right = searchRight(nums, target)
return [left, right]
python
class Solution:
def searchInsert(self, nums: List[int], target: int) -> int:
left = 0
right = len(nums) - 1
while left <= right:
mid = left + (right - left) // 2
if nums[mid] == target:
return mid
elif nums[mid] < target:
left = mid + 1
elif nums[mid] > target:
right = mid - 1
return left
python
class Solution:
def isValidSudoku(self, board: List[List[str]]) -> bool:
hang = []
lie = []
gong = []
for i in range(9):
hang.append([])
lie.append([])
gong.append([])
for i in range(9):
for j in range(9):
num = board[i][j]
if num == ".":
continue
if num in hang[i] or num in lie[j] or num in gong[3 * (i // 3) + 1 * (j // 3)]:
return False
hang[i].append(num)
lie[j].append(num)
gong[3 * (i // 3) + 1 * (j // 3)].append(num)
return True
python
class Solution:
def solveSudoku(self, board: List[List[str]]) -> None:
"""
Do not return anything, modify board in-place instead.
"""
def isValid(board, i, j, n):
for k in range(9):
if n == board[k][j] or n == board[i][k]:
return False
start_row = (i // 3) * 3
start_col = (j // 3) * 3
for i in range(start_row, start_row + 3):
for j in range(start_col, start_col + 3):
if n == board[i][j]:
return False
return True
def backtracking(board):
for i in range(9):
for j in range(9):
if board[i][j] != '.':
continue
for num in range(1, 10):
if isValid(board, i, j, str(num)):
board[i][j] = str(num)
if backtracking(board):
return True
board[i][j] = '.'
return False
return True
backtracking(board)
python
class Solution:
def combinationSum(self, candidates: List[int], target: int) -> List[List[int]]:
def backtracking(candidates, target, start, path, res):
if sum(path) == target:
res.append(path.copy())
return
for i in range(start, len(candidates)):
if candidates[i] + sum(path) > target:
break
path.append(candidates[i])
backtracking(candidates, target, i, path, res)
path.pop()
res = []
path = []
start = 0
candidates.sort()
backtracking(candidates, target, start, path, res)
return res
python
class Solution:
def combinationSum2(self, candidates: List[int], target: int) -> List[List[int]]:
def backtracking(candidates, target, start, path, res):
if sum(path) == target:
res.append(path.copy())
return
for i in range(start, len(candidates)):
if i > start and candidates[i] == candidates[i - 1]:
continue
if candidates[i] + sum(path) > target:
break
path.append(candidates[i])
backtracking(candidates, target, i + 1, path, res)
path.pop()
res = []
path = []
start = 0
candidates.sort()
backtracking(candidates, target, start, path, res)
return res