-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
188 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
### 题目; | ||
在给定的网格中,每个单元格可以有以下三个值之一: | ||
|
||
值 0 代表空单元格; | ||
值 1 代表新鲜橘子; | ||
值 2 代表腐烂的橘子。 | ||
每分钟,任何与腐烂的橘子(在 4 个正方向上)相邻的新鲜橘子都会腐烂。 | ||
|
||
返回直到单元格中没有新鲜橘子为止所必须经过的最小分钟数。如果不可能,返回 -1。 | ||
``` | ||
输入:[[2,1,1],[1,1,0],[0,1,1]] | ||
输出:4 | ||
示例 2: | ||
输入:[[2,1,1],[0,1,1],[1,0,1]] | ||
输出:-1 | ||
解释:左下角的橘子(第 2 行, 第 0 列)永远不会腐烂,因为腐烂只会发生在 4 个正向上。 | ||
``` | ||
|
||
### 解析1: | ||
* **算法流程:** | ||
* 1.将新鲜和腐烂的分开放在两个set中; | ||
* 2.对腐烂的进行遍历,将他的相邻位置全部变成腐烂的; | ||
* 3.更新腐烂的集合为新腐烂的橘子,同时新鲜的减去腐烂的这些; | ||
* 4.如果新腐烂的在fresh之前变成空集,则返回-1; | ||
|
||
|
||
* **复杂度:** | ||
|
||
| |复杂度|大小|百分比| | ||
|--|--|--|--| | ||
|时间|$O(mn)$|60 ms|57.90%| | ||
|空间|$O(mn)$|13.1 MB|17.65%| | ||
|
||
```python | ||
class Solution: | ||
def orangesRotting(self, grid: List[List[int]]) -> int: | ||
m,n = len(grid),len(grid[0]) | ||
|
||
fresh = {(i,j) for i in range(m) for j in range(n) if grid[i][j]==1} | ||
rotten = {(i,j) for i in range(m) for j in range(n) if grid[i][j]==2} | ||
time = 0 | ||
|
||
while fresh: | ||
if not rotten:return -1 | ||
rotten = {(i+dx,j+dy) for i,j in rotten for dx,dy in [(0,1),(0,-1),(1,0),(-1,0)] if (i+dx,j+dy) in fresh} # 一轮后新的rotten,更新才可以 | ||
fresh -= rotten | ||
time += 1 # 多个腐烂的同时开始像两遍腐烂 | ||
|
||
return time | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
### 题目: | ||
给定一个二叉树和一个目标和,找到所有从根节点到叶子节点路径总和等于给定目标和的路径。 | ||
|
||
说明: 叶子节点是指没有子节点的节点。 | ||
|
||
``` | ||
示例: | ||
给定如下二叉树,以及目标和 sum = 22, | ||
5 | ||
/ \ | ||
4 8 | ||
/ / \ | ||
11 13 4 | ||
/ \ / \ | ||
7 2 5 1 | ||
返回: | ||
[ | ||
[5,4,11,2], | ||
[5,8,4,5] | ||
] | ||
``` | ||
|
||
### 解析1: | ||
路径总和,采用递归的方法,其实依然是dfs,在深度优先遍历的时候添加一些限制和变量。 | ||
|
||
* **复杂度:** | ||
| |复杂度|大小|百分比| | ||
|--|--|--|--| | ||
|时间|$O(n)$|56 ms|47.77%| | ||
|空间|$O(n)$|18.8 MB|16.14%| | ||
|
||
* **算法流程:** | ||
1. 深度遍历,dfs,遍历的同时,判断路径是否满足需求; | ||
2. 每次遍历,不只是res.append(root.val),判断是否是叶节点,以及路径和是多少; | ||
|
||
|
||
```python | ||
class Solution(object): | ||
def pathSum(self, root, sum): | ||
|
||
res = [] | ||
if not root: return [] | ||
def dfs(root, sum, tmp): | ||
# sum:到目前结点所需要满足的目标和; | ||
# tmp:到目前结点的结点路径; | ||
if not root: | ||
return | ||
if not root.left and not root.right and sum - root.val == 0 : | ||
tmp += [root.val] | ||
res.append(tmp) | ||
return | ||
dfs(root.left, sum - root.val, tmp + [root.val]) | ||
dfs(root.right, sum - root.val, tmp + [root.val]) | ||
dfs(root, sum, []) | ||
return res | ||
``` | ||
|
||
相比上述代码更加简洁,dfs传入root和temp两个参数。 | ||
|
||
```python | ||
class Solution: | ||
def pathSum(self, root: TreeNode, target: int) -> List[List[int]]: | ||
self.res = [] | ||
def dfs(root, temp): | ||
if not root:return None | ||
if not root.left and not root.right and sum(temp)==target:self.res.append(temp) | ||
if root.left:dfs(root.left,temp+[root.left.val]) | ||
if root.right:dfs(root.right,temp+[root.right.val]) | ||
if not root:return [] | ||
dfs(root,[root.val]) | ||
return self.res | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
### 题目: | ||
输入一个正整数 target ,输出所有和为 target 的连续正整数序列(至少含有两个数)。 | ||
|
||
序列内的数字由小到大排列,不同序列按照首个数字从小到大排列。 | ||
|
||
示例 1: | ||
|
||
输入:target = 9 | ||
输出:[[2,3,4],[4,5]] | ||
示例 2: | ||
|
||
输入:target = 15 | ||
输出:[[1,2,3,4,5],[4,5,6],[7,8]] | ||
|
||
### 解析1: | ||
双指针法,注意阈值停止的条件,和备选空间,备选空间为1到target/2的上取整。 | ||
|
||
* **算法流程:** | ||
* 1.生成备选数组,1到target/2的上取整 | ||
* 2.双指针,遍历nums两个指针间的区间,根据求和大小,分别更新指针; | ||
* 如果出现相等,两个指针都右移; | ||
|
||
|
||
```python | ||
class Solution: | ||
def findContinuousSequence(self, target: int) -> List[List[int]]: | ||
mid = target // 2+1 | ||
nums = [i for i in range(1,mid+1)] | ||
i,j = 0,1 | ||
res = [] | ||
while i<mid and j <= mid: | ||
total = sum(nums[i:j+1]) | ||
if total > target:i+=1 | ||
elif total<target:j+=1 | ||
else: | ||
res.append(nums[i:j+1]) | ||
i+=1 | ||
j+=1 | ||
return res | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
和TwoSum相似,详细记录见主站第一题。 | ||
|
||
```python | ||
class Solution: | ||
def twoSum(self, nums: List[int], target: int) -> List[int]: | ||
n = len(nums) | ||
if n<2:return [] | ||
nums_dict = dict() | ||
for i in range(n): | ||
if nums[i] not in nums_dict: | ||
nums_dict[nums[i]] = i | ||
if target-nums[i] in nums_dict and i != nums_dict[target-nums[i]]: | ||
return [nums[i],target-nums[i]] | ||
|
||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters