Skip to content

Commit

Permalink
2020-03-04
Browse files Browse the repository at this point in the history
  • Loading branch information
linxid committed Mar 5, 2020
1 parent 7f5cc24 commit 328cded
Show file tree
Hide file tree
Showing 6 changed files with 188 additions and 4 deletions.
51 changes: 51 additions & 0 deletions 994. 腐烂的橘子.md
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
```
9 changes: 5 additions & 4 deletions python/113. 路径总和 II.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,14 @@
### 解析1:
路径总和,采用递归的方法,其实依然是dfs,在深度优先遍历的时候添加一些限制和变量。

* **复杂度:**
| |复杂度|大小|百分比|
|--|--|--|--|
|时间|$O(n)$|68 ms|10.77%|
|空间|$O(n)$|18.4 MB|10.14%|
|时间|$O(n)$|56 ms|47.77%|
|空间|$O(n)$|18.8 MB|16.14%|

步骤:
1. 深度遍历,dfs辅助函数,类似前序遍历
* **算法流程:**
1. 深度遍历,dfs,遍历的同时,判断路径是否满足需求
2. 每次遍历,不只是res.append(root.val),判断是否是叶节点,以及路径和是多少;


Expand Down
74 changes: 74 additions & 0 deletions 剑指offer/面试题34. 二叉树中和为某一值的路径.md
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
```
40 changes: 40 additions & 0 deletions 剑指offer/面试题57 - II. 和为s的连续正数序列.md
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
```
15 changes: 15 additions & 0 deletions 剑指offer/面试题57. 和为s的两个数字.md
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]]

```
3 changes: 3 additions & 0 deletions 剑指offer/题目记录.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,13 @@
面试题32-I. 从上到下打印二叉树|Easy|$O(n)$|二叉树|Done|此处不需要分层保存,所以不停遍历即可
面试题32-II. 从上到下打印二叉树II|Easy|$O(n)$|二叉树|Done|分层保存遍历,需要两层循环
面试题32-III. 从上到下打印二叉树III|Easy|$O(n)$|二叉树|Done|level来判断正序还是逆序保存,奇偶层分开保存
面试题34. 二叉树中和为某一值的路径|Medium|$O(n)$|二叉树|Done|dfs遍历的同时,判断是否是叶节点以及路径和为target
面试题39. 数组中出现次数超过一半的数字|Easy|$O(n)$|数组|Done|排序返回中间那个数;字典保存出现次数,再次遍历
面试题50. 第一个只出现一次的字符|Easy|$O(n)$|字符串|Done|求次数,遍历即可
面试题55 - I. 二叉树的深度|Easy|$O(n)$|二叉树|Done|递归求左右子树的深度,然后返回较大值+1;
面试题55 - II. 平衡二叉树|Easy|$O(n)$|二叉树|No|辅助函数,dfs遍历树,求树深的同时,判断树是否是平衡的
面试题57.和为s的两个数字|Easy|$O(n)$|数组,set|Done|字典保存数和其索引
面试题57 - II. 和为s的连续正数序列|Easy|$O(n)$|双指针|No|建立备选数组,然后双指针遍历



Expand Down

0 comments on commit 328cded

Please sign in to comment.