LeetCode 中等题解(3)
34 在排序数组中查找元素的第一个和最后一个位置
Question
给定一个按照升序排列的整数数组 nums
,和一个目标值 target
。找出给定目标值在数组中的开始位置和结束位置。
你的算法时间复杂度必须是 O(log n) 级别。
如果数组中不存在目标值,返回 [-1, -1]
。
示例 1:
输入: nums = [5,7,7,8,8,10], target = 8
输出: [3,4]
示例 2:
输入: nums = [5,7,7,8,8,10], target = 6
输出: [-1,-1]
Answer
#
# @lc app=leetcode.cn id=34 lang=python3
#
# [34] 在排序数组中查找元素的第一个和最后一个位置
#
# @lc code=start
class Solution:
def searchRange(self, nums: List[int], target: int) -> List[int]:
start = 0
end = len(nums) - 1
# 空数组的情况
if end < 0:
return [-1, -1]
# 二分查找target落在的位置
while (start < end):
if nums[start] == target:
end = start
break
if nums[end] == target:
start = end
break
mid = (start + end) // 2
if nums[mid] < target:
start = mid + 1
if nums[mid] > target:
end = mid - 1
if nums[mid] == target:
start = mid
end = mid
break
# 如果找不到target
if nums[start] != target:
return [-1, -1]
# 找到target,向前向后搜索边界
while (start > 0 and nums[start - 1] == target):
start -= 1
while (end < len(nums) - 1 and nums[end + 1] == target):
end += 1
return [start, end]
# @lc code=end
36 有效的数独
Question
判断一个 9x9 的数独是否有效。只需要根据以下规则,验证已经填入的数字是否有效即可。
- 数字
1-9
在每一行只能出现一次。 - 数字
1-9
在每一列只能出现一次。 - 数字
1-9
在每一个以粗实线分隔的3x3
宫内只能出现一次。
上图是一个部分填充的有效的数独。
数独部分空格内已填入了数字,空白格用 '.'
表示。
示例 1:
输入:
[
["5","3",".",".","7",".",".",".","."],
["6",".",".","1","9","5",".",".","."],
[".","9","8",".",".",".",".","6","."],
["8",".",".",".","6",".",".",".","3"],
["4",".",".","8",".","3",".",".","1"],
["7",".",".",".","2",".",".",".","6"],
[".","6",".",".",".",".","2","8","."],
[".",".",".","4","1","9",".",".","5"],
[".",".",".",".","8",".",".","7","9"]
]
输出: true
示例 2:
输入:
[
["8","3",".",".","7",".",".",".","."],
["6",".",".","1","9","5",".",".","."],
[".","9","8",".",".",".",".","6","."],
["8",".",".",".","6",".",".",".","3"],
["4",".",".","8",".","3",".",".","1"],
["7",".",".",".","2",".",".",".","6"],
[".","6",".",".",".",".","2","8","."],
[".",".",".","4","1","9",".",".","5"],
[".",".",".",".","8",".",".","7","9"]
]
输出: false
解释: 除了第一行的第一个数字从 5 改为 8 以外,空格内其他数字均与 示例1 相同。
但由于位于左上角的 3x3 宫内有两个 8 存在, 因此这个数独是无效的。
说明:
- 一个有效的数独(部分已被填充)不一定是可解的。
- 只需要根据以上规则,验证已经填入的数字是否有效即可。
- 给定数独序列只包含数字
1-9
和字符'.'
。 - 给定数独永远是
9x9
形式的。
Answer
#
# @lc app=leetcode.cn id=36 lang=python3
#
# [36] 有效的数独
#
# @lc code=start
class Solution:
def isValidSudoku(self, board: List[List[str]]) -> bool:
import numpy as np
board = np.array(board)
row = np.zeros((10, 10))
column = np.zeros((10, 10))
group = np.zeros((10, 10))
for i in range(9):
for j in range(9):
if board[i, j] == '.':
continue
num = int(board[i, j])
if row[i][num] == 0 and column[j][num] == 0 and group[i // 3 * 3 + j // 3][num] == 0:
row[i][num] = 1
column[j][num] = 1
group[i // 3 * 3 + j // 3][num] = 1
else:
return False
return True
# @lc code=end
39 组合总和
Question
给定一个无重复元素的数组 candidates
和一个目标数 target
,找出 candidates
中所有可以使数字和为 target
的组合。
candidates
中的数字可以无限制重复被选取。
说明:
- 所有数字(包括
target
)都是正整数。 - 解集不能包含重复的组合。
示例 1:
输入: candidates = [2,3,6,7], target = 7,
所求解集为:
[
[7],
[2,2,3]
]
示例 2:
输入: candidates = [2,3,5], target = 8,
所求解集为:
[
[2,2,2,2],
[2,3,3],
[3,5]
]
Answer
#
# @lc app=leetcode.cn id=39 lang=python3
#
# [39] 组合总和
#
# @lc code=start
class Solution:
def combinationSum(self, candidates: List[int], target: int) -> List[List[int]]:
size = len(candidates)
if size == 0:
return []
# 剪枝是为了提速,在本题非必需
candidates.sort()
# 在遍历的过程中记录路径,它是一个栈
path = []
res = []
# 注意要传入 size ,在 range 中, size 取不到
self.__dfs(candidates, 0, size, path, res, target)
return res
def __dfs(self, candidates, begin, size, path, res, target):
# 先写递归终止的情况
if target == 0:
# Python 中可变对象是引用传递,因此需要将当前 path 里的值拷贝出来
# 或者使用 path.copy()
res.append(path[:])
return
for index in range(begin, size):
residue = target - candidates[index]
# “剪枝”操作,不必递归到下一层,并且后面的分支也不必执行
if residue < 0:
break
path.append(candidates[index])
# 因为下一层不能比上一层还小,起始索引还从 index 开始
self.__dfs(candidates, index, size, path, res, residue)
path.pop()
# @lc code=end
___________________
文章转载请注明出处,喜欢文章的话请我喝一杯咖啡吧!在右边赞助里面哦!谢谢! NoMornings.
文章转载请注明出处,喜欢文章的话请我喝一杯咖啡吧!在右边赞助里面哦!谢谢! NoMornings.