Skip to content

Commit 24201c2

Browse files
Merge branch 'master' of github.com:youngyangyang04/leetcode-master
2 parents 82e12be + 61c21cc commit 24201c2

12 files changed

+227
-67
lines changed

problems/0001.两数之和.md

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -402,16 +402,18 @@ public class Solution {
402402
### Dart:
403403

404404
```dart
405-
List<int> twoSum(List<int> nums, int target) {
406-
var tmp = [];
407-
for (var i = 0; i < nums.length; i++) {
408-
var rest = target - nums[i];
409-
if(tmp.contains(rest)){
410-
return [tmp.indexOf(rest), i];
411-
}
412-
tmp.add(nums[i]);
405+
import 'dart:collection';
406+
407+
List<int> twoSum(List<int> nums, int target) {
408+
HashMap<int, int> hashMap = HashMap();
409+
for (int i = 0; i < nums.length; i++) {
410+
int rest = target - nums[i];
411+
if (hashMap.containsKey(rest)) {
412+
return [hashMap[rest]!, i];
413413
}
414-
return [0 , 0];
414+
hashMap.addEntries({nums[i]: i}.entries);
415+
}
416+
return [];
415417
}
416418
```
417419

problems/0024.两两交换链表中的节点.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ class Solution {
5151
public:
5252
ListNode* swapPairs(ListNode* head) {
5353
ListNode* dummyHead = new ListNode(0); // 设置一个虚拟头结点
54-
dummyHead->next = head; // 将虚拟头结点指向head,这样方面后面做删除操作
54+
dummyHead->next = head; // 将虚拟头结点指向head,这样方便后面做删除操作
5555
ListNode* cur = dummyHead;
5656
while(cur->next != nullptr && cur->next->next != nullptr) {
5757
ListNode* tmp = cur->next; // 记录临时节点
@@ -160,7 +160,7 @@ class Solution {
160160
class Solution {
161161
public ListNode swapPairs(ListNode head) {
162162
ListNode dumyhead = new ListNode(-1); // 设置一个虚拟头结点
163-
dumyhead.next = head; // 将虚拟头结点指向head,这样方面后面做删除操作
163+
dumyhead.next = head; // 将虚拟头结点指向head,这样方便后面做删除操作
164164
ListNode cur = dumyhead;
165165
ListNode temp; // 临时节点,保存两个节点后面的节点
166166
ListNode firstnode; // 临时节点,保存两个节点之中的第一个节点

problems/0027.移除元素.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,26 @@ class Solution {
197197
}
198198
```
199199

200+
```java
201+
// 相向双指针法(版本二)
202+
class Solution {
203+
public int removeElement(int[] nums, int val) {
204+
int left = 0;
205+
int right = nums.length - 1;
206+
while(left <= right){
207+
if(nums[left] == val){
208+
nums[left] = nums[right];
209+
right--;
210+
}else {
211+
// 这里兼容了right指针指向的值与val相等的情况
212+
left++;
213+
}
214+
}
215+
return left;
216+
}
217+
}
218+
```
219+
200220
### Python:
201221

202222

problems/0054.螺旋矩阵.md

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,60 @@ var spiralOrder = function(matrix) {
251251
return arr
252252
};
253253
```
254+
### Python
255+
256+
```python
257+
class Solution(object):
258+
def spiralOrder(self, matrix):
259+
"""
260+
:type matrix: List[List[int]]
261+
:rtype: List[int]
262+
"""
263+
if len(matrix) == 0 or len(matrix[0]) == 0 : # 判定List是否为空
264+
return []
265+
row, col = len(matrix), len(matrix[0]) # 行数,列数
266+
loop = min(row, col) // 2 # 循环轮数
267+
stx, sty = 0, 0 # 起始x,y坐标
268+
i, j =0, 0
269+
count = 0 # 计数
270+
offset = 1 # 每轮减少的格子数
271+
result = [0] * (row * col)
272+
while loop>0 :# 左闭右开
273+
i, j = stx, sty
274+
while j < col - offset : # 从左到右
275+
result[count] = matrix[i][j]
276+
count += 1
277+
j += 1
278+
while i < row - offset : # 从上到下
279+
result[count] = matrix[i][j]
280+
count += 1
281+
i += 1
282+
while j>sty : # 从右到左
283+
result[count] = matrix[i][j]
284+
count += 1
285+
j -= 1
286+
while i>stx : # 从下到上
287+
result[count] = matrix[i][j]
288+
count += 1
289+
i -= 1
290+
stx += 1
291+
sty += 1
292+
offset += 1
293+
loop -= 1
294+
if min(row, col) % 2 == 1 : # 判定是否需要填充多出来的一行
295+
i = stx
296+
if row < col :
297+
while i < stx + col - row + 1 :
298+
result[count] = matrix[stx][i]
299+
count += 1
300+
i += 1
301+
else :
302+
while i < stx + row - col + 1 :
303+
result[count] = matrix[i][stx]
304+
count += 1
305+
i += 1
306+
return result
307+
```
254308

255309

256310

problems/0200.岛屿数量.深搜版.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,40 @@ class Solution:
278278

279279
return result
280280
```
281+
```python
282+
# 我们用三个状态去标记每一个格子
283+
# 0 代表海水
284+
# 1 代表陆地
285+
# 2 代表已经访问的陆地
286+
class Solution:
287+
def traversal(self, grid, i, j):
288+
m = len(grid)
289+
n = len(grid[0])
290+
291+
if i < 0 or j < 0 or i >= m or j >= n:
292+
return # 越界了
293+
elif grid[i][j] == "2" or grid[i][j] == "0":
294+
return
295+
296+
grid[i][j] = "2"
297+
self.traversal(grid, i - 1, j) # 往上走
298+
self.traversal(grid, i + 1, j) # 往下走
299+
self.traversal(grid, i, j - 1) # 往左走
300+
self.traversal(grid, i, j + 1) # 往右走
301+
302+
def numIslands(self, grid: List[List[str]]) -> int:
303+
res = 0
304+
305+
306+
for i in range(len(grid)):
307+
for j in range(len(grid[0])):
308+
if grid[i][j] == "1":
309+
res += 1
310+
self.traversal(grid, i, j)
311+
312+
return res
313+
```
314+
281315
### JavaScript
282316

283317
```javascript

problems/0203.移除链表元素.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ class Solution {
127127
public:
128128
ListNode* removeElements(ListNode* head, int val) {
129129
ListNode* dummyHead = new ListNode(0); // 设置一个虚拟头结点
130-
dummyHead->next = head; // 将虚拟头结点指向head,这样方面后面做删除操作
130+
dummyHead->next = head; // 将虚拟头结点指向head,这样方便后面做删除操作
131131
ListNode* cur = dummyHead;
132132
while (cur->next != NULL) {
133133
if(cur->next->val == val) {

problems/0213.打家劫舍II.md

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -225,32 +225,38 @@ class Solution:
225225
// 打家劫舍Ⅱ 动态规划
226226
// 时间复杂度O(n) 空间复杂度O(n)
227227
func rob(nums []int) int {
228-
if len(nums) == 1 {
229-
return nums[0]
228+
// 如果长度为0或1,那么有没有环的限制都一样
229+
if len(nums) <= 1 {
230+
return robWithoutCircle(nums)
230231
}
231-
if len(nums) == 2 {
232-
return max(nums[0], nums[1])
233-
}
234-
235-
result1 := robRange(nums, 0)
236-
result2 := robRange(nums, 1)
237-
return max(result1, result2)
232+
233+
// 否则,去头或去尾,取最大
234+
res1 := robWithoutCircle(nums[:len(nums)-1])
235+
res2 := robWithoutCircle(nums[1:])
236+
237+
return max(res1, res2)
238238
}
239239

240-
// 偷盗指定的范围
241-
func robRange(nums []int, start int) int {
240+
// 原始的打家劫舍版
241+
func robWithoutCircle(nums []int) int {
242+
switch len(nums) {
243+
case 0: return 0
244+
case 1: return nums[0]
245+
}
242246
dp := make([]int, len(nums))
243-
dp[1] = nums[start]
244-
245-
for i := 2; i < len(nums); i++ {
246-
dp[i] = max(dp[i - 2] + nums[i - 1 + start], dp[i - 1])
247+
dp[0]=nums[0]
248+
dp[1] = max(nums[0], nums[1])
249+
250+
for i:=2; i<len(nums); i++ {
251+
dp[i] = max(dp[i-1], dp[i-2]+nums[i])
247252
}
248-
249-
return dp[len(nums) - 1]
253+
254+
return dp[len(nums)-1]
255+
250256
}
251257

252-
func max(a, b int) int {
253-
if a > b {
258+
func max(a, b int ) int {
259+
if a>b {
254260
return a
255261
}
256262
return b

problems/0392.判断子序列.md

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -240,26 +240,45 @@ function isSubsequence(s: string, t: string): boolean {
240240

241241
### Go:
242242

243+
二维DP:
244+
243245
```go
244246
func isSubsequence(s string, t string) bool {
245-
dp := make([][]int,len(s)+1)
246-
for i:=0;i<len(dp);i++{
247-
dp[i] = make([]int,len(t)+1)
247+
dp := make([][]int, len(s) + 1)
248+
for i := 0; i < len(dp); i ++{
249+
dp[i] = make([]int, len(t) + 1)
248250
}
249-
for i:=1;i<len(dp);i++{
250-
for j:=1;j<len(dp[i]);j++{
251-
if s[i-1] == t[j-1]{
252-
dp[i][j] = dp[i-1][j-1] +1
251+
for i := 1; i < len(dp); i ++{
252+
for j := 1; j < len(dp[i]); j ++{
253+
if s[i - 1] == t[j - 1] {
254+
dp[i][j] = dp[i - 1][j - 1] +1
253255
}else{
254-
dp[i][j] = dp[i][j-1]
256+
dp[i][j] = dp[i][j - 1]
255257
}
256258
}
257259
}
258-
return dp[len(s)][len(t)]==len(s)
260+
return dp[len(s)][len(t)] == len(s)
259261
}
260262
```
261263

262-
Rust:
264+
一维DP:
265+
266+
```go
267+
func isSubsequence(s string, t string) bool {
268+
dp := make([]int, len(s) + 1)
269+
for i := 1; i <= len(t); i ++ {
270+
for j := len(s); j >= 1; j -- {
271+
if t[i - 1] == s[j - 1] {
272+
dp[j] = dp[j - 1] + 1
273+
}
274+
}
275+
}
276+
return dp[len(s)] == len(s)
277+
}
278+
```
279+
280+
281+
### Rust:
263282

264283
```rust
265284
impl Solution {

problems/0450.删除二叉搜索树中的节点.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -769,6 +769,38 @@ impl Solution {
769769
}
770770
```
771771

772+
### C#
773+
774+
> 递归法:
775+
```C#
776+
public TreeNode DeleteNode(TreeNode root, int key) {
777+
// 第一种情况:没找到删除的节点,遍历到空节点直接返回了
778+
if (root == null) return null;
779+
if(key == root.val) {
780+
//第二种情况:左右孩子都为空(叶子节点),直接删除节点, 返回NULL为根节点
781+
if(root.left == null && root.right == null) return null;
782+
//第三种情况:其左孩子为空,右孩子不为空,删除节点,右孩子补位 ,返回右孩子为根节点
783+
if (root.left == null && root.right != null) return root.right;
784+
//第四种情况:其右孩子为空,左孩子不为空,删除节点,左孩子补位,返回左孩子为根节点
785+
if (root.left != null && root.right == null) return root.left;
786+
//第五种情况:左右孩子节点都不为空,则将删除节点的左子树放到删除节点的右子树的最左面节点的左孩子的位置
787+
// 并返回删除节点右孩子为新的根节点。
788+
if(root.left != null && root.right != null) {
789+
TreeNode leftNode = root.right; // 找右子树最左面的节点
790+
while(leftNode.left != null)
791+
leftNode = leftNode.left;
792+
leftNode.left = root.left; // 把要删除的节点(root)左子树放在leftNode的左孩子的位置
793+
return root.right; // 返回旧root的右孩子作为新root
794+
}
795+
}
796+
797+
if(root.val > key) root.left = DeleteNode(root.left, key);
798+
if(root.val < key) root.right = DeleteNode(root.right, key);
799+
800+
return root;
801+
}
802+
```
803+
772804
<p align="center">
773805
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
774806
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>

problems/0797.所有可能的路径.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ if (x == graph.size() - 1) { // 找到符合条件的一条路径
8989
for (int i = 0; i < graph[x].size(); i++) { // 遍历节点n链接的所有节点
9090
```
9191
92-
接下来就是将 选中的x所连接的节点,加入到 单一路劲来
92+
接下来就是将 选中的x所连接的节点,加入到 单一路径来
9393
9494
```C++
9595
path.push_back(graph[x][i]); // 遍历到的节点加入到路径中来

0 commit comments

Comments
 (0)