Skip to content

Commit 3704f7b

Browse files
author
lucifer
committed
2 parents 980d576 + 81f4b13 commit 3704f7b

File tree

4 files changed

+108
-126
lines changed

4 files changed

+108
-126
lines changed

problems/200.number-of-islands.md

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ Output: 3
5353

5454
## 代码
5555

56-
* 语言支持:JS
56+
* 语言支持:JS, python3
5757

5858
Javascript Code:
5959
```js
@@ -94,5 +94,32 @@ var numIslands = function(grid) {
9494
};
9595
```
9696

97+
python code:
98+
99+
``` python
100+
class Solution:
101+
def numIslands(self, grid: List[List[str]]) -> int:
102+
if not grid: return 0
103+
104+
count = 0
105+
for i in range(len(grid)):
106+
for j in range(len(grid[0])):
107+
if grid[i][j] == '1':
108+
self.dfs(grid, i, j)
109+
count += 1
110+
111+
return count
112+
113+
def dfs(self, grid, i, j):
114+
if i < 0 or j < 0 or i >= len(grid) or j >= len(grid[0]) or grid[i][j] != '1':
115+
return
116+
grid[i][j] = '0'
117+
self.dfs(grid, i + 1, j)
118+
self.dfs(grid, i - 1, j)
119+
self.dfs(grid, i, j + 1)
120+
self.dfs(grid, i, j - 1)
121+
122+
```
123+
97124

98125

problems/236.lowest-common-ancestor-of-a-binary-tree.md

Lines changed: 29 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -70,60 +70,11 @@ p and q are different and both values will exist in the binary tree.
7070

7171
## 代码
7272

73-
```js
73+
代码支持: JavaScript, Python3
7474

75+
- JavaScript Code:
7576

76-
/*
77-
* @lc app=leetcode id=236 lang=javascript
78-
*
79-
* [236] Lowest Common Ancestor of a Binary Tree
80-
*
81-
* https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tree/description/
82-
*
83-
* algorithms
84-
* Medium (35.63%)
85-
* Total Accepted: 267.3K
86-
* Total Submissions: 729.2K
87-
* Testcase Example: '[3,5,1,6,2,0,8,null,null,7,4]\n5\n1'
88-
*
89-
* Given a binary tree, find the lowest common ancestor (LCA) of two given
90-
* nodes in the tree.
91-
*
92-
* According to the definition of LCA on Wikipedia: “The lowest common ancestor
93-
* is defined between two nodes p and q as the lowest node in T that has both p
94-
* and q as descendants (where we allow a node to be a descendant of itself).”
95-
*
96-
* Given the following binary tree:  root = [3,5,1,6,2,0,8,null,null,7,4]
97-
*
98-
*
99-
*
100-
* Example 1:
101-
*
102-
*
103-
* Input: root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 1
104-
* Output: 3
105-
* Explanation: The LCA of nodes 5 and 1 is 3.
106-
*
107-
*
108-
* Example 2:
109-
*
110-
*
111-
* Input: root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 4
112-
* Output: 5
113-
* Explanation: The LCA of nodes 5 and 4 is 5, since a node can be a descendant
114-
* of itself according to the LCA definition.
115-
*
116-
*
117-
*
118-
*
119-
* Note:
120-
*
121-
*
122-
* All of the nodes' values will be unique.
123-
* p and q are different and both values will exist in the binary tree.
124-
*
125-
*
126-
*/
77+
```js
12778
/**
12879
* Definition for a binary tree node.
12980
* function TreeNode(val) {
@@ -147,6 +98,32 @@ var lowestCommonAncestor = function(root, p, q) {
14798
};
14899
```
149100

101+
- Python Code:
102+
103+
``` python
104+
# Definition for a binary tree node.
105+
# class TreeNode:
106+
# def __init__(self, x):
107+
# self.val = x
108+
# self.left = None
109+
# self.right = None
110+
111+
class Solution:
112+
def lowestCommonAncestor(self, root: 'TreeNode', p: 'TreeNode', q: 'TreeNode') -> 'TreeNode':
113+
if not root or root == p or root == q:
114+
return root
115+
left = self.lowestCommonAncestor(root.left, p, q)
116+
right = self.lowestCommonAncestor(root.right, p, q)
117+
118+
if not left:
119+
return right
120+
if not right:
121+
return left
122+
else:
123+
return root
124+
125+
```
126+
150127
## 扩展
151128
如果递归的结束条件改为`if (!root || root.left === p || root.right === q) return root;` 代表的是什么意思,对结果有什么样的影响?
152129

problems/75.sort-colors.md

Lines changed: 50 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -19,77 +19,55 @@ First, iterate the array counting number of 0's, 1's, and 2's, then overwrite ar
1919
Could you come up with a one-pass algorithm using only constant space?
2020

2121
## 思路
22+
这个问题是典型的荷兰国旗问题 (https://en.wikipedia.org/wiki/Dutch_national_flag_problem)。 因为我们可以将红白蓝三色小球想象成条状物,有序排列后正好组成荷兰国旗。
2223

23-
其实就是排序,而且没有要求稳定性,就是用啥排序算法都行。
24-
题目并没有给出数据规模,因此我默认数据量不大,直接选择了冒泡排序
25-
26-
## 关键点解析
27-
28-
冒泡排序的时间复杂度是N平方,无法优化,但是可以进一步优化常数项,
29-
比如循环的起止条件。 由于每一次遍历都会将最后一位“就位”,因此内层循环的截止条件就可以是
30-
`nums.length - i`, 而不是 `nums.length`, 可以省一半的时间。
31-
32-
33-
## 代码
34-
35-
```js
36-
/*
37-
* @lc app=leetcode id=75 lang=javascript
38-
*
39-
* [75] Sort Colors
40-
*
41-
* https://leetcode.com/problems/sort-colors/description/
42-
*
43-
* algorithms
44-
* Medium (41.41%)
45-
* Total Accepted: 297K
46-
* Total Submissions: 716.1K
47-
* Testcase Example: '[2,0,2,1,1,0]'
48-
*
49-
* Given an array with n objects colored red, white or blue, sort them in-place
50-
* so that objects of the same color are adjacent, with the colors in the order
51-
* red, white and blue.
52-
*
53-
* Here, we will use the integers 0, 1, and 2 to represent the color red,
54-
* white, and blue respectively.
55-
*
56-
* Note: You are not suppose to use the library's sort function for this
57-
* problem.
58-
*
59-
* Example:
60-
*
61-
*
62-
* Input: [2,0,2,1,1,0]
63-
* Output: [0,0,1,1,2,2]
64-
*
65-
* Follow up:
66-
*
67-
*
68-
* A rather straight forward solution is a two-pass algorithm using counting
69-
* sort.
70-
* First, iterate the array counting number of 0's, 1's, and 2's, then
71-
* overwrite array with total number of 0's, then 1's and followed by 2's.
72-
* Could you come up with a one-pass algorithm using only constant space?
73-
*
74-
*
75-
*/
76-
/**
77-
* @param {number[]} nums
78-
* @return {void} Do not return anything, modify nums in-place instead.
79-
*/
80-
var sortColors = function(nums) {
81-
function swap(nums, i, j) {
82-
const temp = nums[i];
83-
nums[i] = nums[j];
84-
nums[j] = temp;
85-
}
86-
for (let i = 0; i < nums.length - 1; i++) {
87-
for (let j = 0; j < nums.length - i; j++) {
88-
if (nums[j] < nums[j -1]) {
89-
swap(nums, j - 1 , j)
90-
}
91-
92-
}
93-
}
94-
};
24+
有两种解决思路。
25+
26+
## 解法一
27+
- 遍历数组,统计红白蓝三色球(0,1,2)的个数
28+
- 根据红白蓝三色球(0,1,2)的个数重排数组
29+
30+
这种思路的时间复杂度:$O(n)$,需要遍历数组两次。
31+
32+
## 解法二
33+
34+
我们可以把数组分成三部分,前部(全部是0),中部(全部是1)和后部(全部是2)三个部分。每一个元素(红白蓝分别对应0、1、2)必属于其中之一。将前部和后部各排在数组的前边和后边,中部自然就排好了。
35+
36+
我们用三个指针,设置两个指针begin指向前部的末尾的下一个元素(刚开始默认前部无0,所以指向第一个位置),end指向后部开头的前一个位置(刚开始默认后部无2,所以指向最后一个位置),然后设置一个遍历指针current,从头开始进行遍历。
37+
38+
这种思路的时间复杂度也是$O(n)$, 只需要遍历数组一次。
39+
40+
### 关键点解析
41+
42+
43+
- 荷兰国旗问题
44+
- counting sort
45+
46+
### 代码
47+
48+
代码支持: Python3
49+
50+
Python3 Code:
51+
52+
``` python
53+
class Solution:
54+
def sortColors(self, nums: List[int]) -> None:
55+
"""
56+
Do not return anything, modify nums in-place instead.
57+
"""
58+
p0 = cur = 0
59+
p2 = len(nums) - 1
60+
61+
while cur <= p2:
62+
if nums[cur] == 0:
63+
nums[cur], nums[p0] = nums[p0], nums[cur]
64+
p0 += 1
65+
cur += 1
66+
elif nums[cur] == 2:
67+
nums[cur], nums[p2] = nums[p2], nums[cur]
68+
p2 -= 1
69+
else:
70+
cur += 1
9571
```
72+
73+

problems/80.remove-duplicates-from-sorted-array-ii.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ for (int i = 0; i < len; i++) {
5555

5656
- 初始化快慢指针 slow , fast ,全部指向索引为 0 的元素。
5757
- fast 每次移动一格
58-
- 满指针选择性移动,即只有写入数据之后才移动。是否写入数据取决于 slow - 2 对应的数字和 fast 对应的数字是否一致。
58+
- 慢指针选择性移动,即只有写入数据之后才移动。是否写入数据取决于 slow - 2 对应的数字和 fast 对应的数字是否一致。
5959
- 如果一致,我们不应该写。 否则我们就得到了三个相同的数字,不符合题意
6060
- 如果不一致,我们需要将 fast 指针的数据写入到 slow 指针。
6161
- 重复这个过程,直到 fast 走到头,说明我们已无数字可写。

0 commit comments

Comments
 (0)