Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

[pull] master from youngyangyang04:master #301

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
pull merged 9 commits into AlgorithmAndLeetCode:master from youngyangyang04:master
Jul 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions problems/0027.移除元素.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,12 @@

**你不需要考虑数组中超出新长度后面的元素。**

## 思路

针对本题,我录制了视频讲解:[数组中移除元素并不容易!LeetCode:27. 移除元素](https://www.bilibili.com/video/BV12A4y1Z7LP),结合本题解一起看,事半功倍!
## 算法公开课

**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[数组中移除元素并不容易!LeetCode:27. 移除元素](https://www.bilibili.com/video/BV12A4y1Z7LP),相信结合视频再看本篇题解,更有助于大家对本题的理解**。

## 思路

有的同学可能说了,多余的元素,删掉不就得了。

Expand Down
9 changes: 6 additions & 3 deletions problems/0059.螺旋矩阵II.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@



## 59.螺旋矩阵II
# 59.螺旋矩阵II

[力扣题目链接](https://leetcode.cn/problems/spiral-matrix-ii/)

Expand All @@ -22,9 +22,12 @@
[ 7, 6, 5 ]
]

## 思路

为了利于录友们理解,我特意录制了视频,[拿下螺旋矩阵!LeetCode:59.螺旋矩阵II](https://www.bilibili.com/video/BV1SL4y1N7mV),结合视频一起看,事半功倍!
## 算法公开课

**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[拿下螺旋矩阵!LeetCode:59.螺旋矩阵II](https://www.bilibili.com/video/BV1SL4y1N7mV),相信结合视频再看本篇题解,更有助于大家对本题的理解**。

## 思路

这道题目可以说在面试中出现频率较高的题目,**本题并不涉及到什么算法,就是模拟过程,但却十分考察对代码的掌控能力。**

Expand Down
89 changes: 89 additions & 0 deletions problems/0130.被围绕的区域.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,54 @@ class Solution {
}
}
```
```Java
//BFS(使用helper function)
class Solution {
int[][] dir ={{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
public void solve(char[][] board) {
for(int i = 0; i < board.length; i++){
if(board[i][0] == 'O') bfs(board, i, 0);
if(board[i][board[0].length - 1] == 'O') bfs(board, i, board[0].length - 1);
}

for(int j = 1 ; j < board[0].length - 1; j++){
if(board[0][j] == 'O') bfs(board, 0, j);
if(board[board.length - 1][j] == 'O') bfs(board, board.length - 1, j);
}

for(int i = 0; i < board.length; i++){
for(int j = 0; j < board[0].length; j++){
if(board[i][j] == 'O') board[i][j] = 'X';
if(board[i][j] == 'A') board[i][j] = 'O';
}
}
}
private void bfs(char[][] board, int x, int y){
Queue<Integer> que = new LinkedList<>();
board[x][y] = 'A';
que.offer(x);
que.offer(y);

while(!que.isEmpty()){
int currX = que.poll();
int currY = que.poll();

for(int i = 0; i < 4; i++){
int nextX = currX + dir[i][0];
int nextY = currY + dir[i][1];

if(nextX < 0 || nextY < 0 || nextX >= board.length || nextY >= board[0].length)
continue;
if(board[nextX][nextY] == 'X'|| board[nextX][nextY] == 'A')
continue;
bfs(board, nextX, nextY);
}
}
}
}

```

```Java
// 深度优先遍历
// 使用 visited 数组进行标记
Expand Down Expand Up @@ -296,6 +344,47 @@ class Solution {
}
}
```
```java
//DFS(有終止條件)
class Solution {
int[][] dir ={{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
public void solve(char[][] board) {

for(int i = 0; i < board.length; i++){
if(board[i][0] == 'O') dfs(board, i, 0);
if(board[i][board[0].length - 1] == 'O') dfs(board, i, board[0].length - 1);
}

for(int j = 1 ; j < board[0].length - 1; j++){
if(board[0][j] == 'O') dfs(board, 0, j);
if(board[board.length - 1][j] == 'O') dfs(board, board.length - 1, j);
}

for(int i = 0; i < board.length; i++){
for(int j = 0; j < board[0].length; j++){
if(board[i][j] == 'O') board[i][j] = 'X';
if(board[i][j] == 'A') board[i][j] = 'O';
}
}
}

private void dfs(char[][] board, int x, int y){
if(board[x][y] == 'X'|| board[x][y] == 'A')
return;
board[x][y] = 'A';
for(int i = 0; i < 4; i++){
int nextX = x + dir[i][0];
int nextY = y + dir[i][1];

if(nextX < 0 || nextY < 0 || nextX >= board.length || nextY >= board[0].length)
continue;
// if(board[nextX][nextY] == 'X'|| board[nextX][nextY] == 'A')
// continue;
dfs(board, nextX, nextY);
}
}
}
```

<p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
Expand Down
4 changes: 4 additions & 0 deletions problems/0203.移除链表元素.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@
输入:head = [7,7,7,7], val = 7
输出:[]

# 算法公开课

**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[链表基础操作| LeetCode:203.移除链表元素](https://www.bilibili.com/video/BV18B4y1s7R9),相信结合视频再看本篇题解,更有助于大家对本题的理解**。


# 思路

Expand Down
5 changes: 5 additions & 0 deletions problems/0206.翻转链表.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@
输入: 1->2->3->4->5->NULL
输出: 5->4->3->2->1->NULL

# 算法公开课

**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[帮你拿下反转链表 | LeetCode:206.反转链表](https://www.bilibili.com/video/BV1nB4y1i7eL),相信结合视频再看本篇题解,更有助于大家对本题的理解**。


# 思路

本题我录制了B站视频,[帮你拿下反转链表 | LeetCode:206.反转链表](https://www.bilibili.com/video/BV1nB4y1i7eL),相信结合视频在看本篇题解,更有助于大家对链表的理解。
Expand Down
13 changes: 8 additions & 5 deletions problems/0209.长度最小的子数组.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,22 @@

示例:

输入:s = 7, nums = [2,3,1,2,4,3]
输出:2
解释:子数组 [4,3] 是该条件下的长度最小的子数组。
* 输入:s = 7, nums = [2,3,1,2,4,3]
* 输出:2
* 解释:子数组 [4,3] 是该条件下的长度最小的子数组。

提示:

* 1 <= target <= 10^9
* 1 <= nums.length <= 10^5
* 1 <= nums[i] <= 10^5

# 思路
# 算法公开课

**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[拿下滑动窗口! | LeetCode 209 长度最小的子数组](https://www.bilibili.com/video/BV1tZ4y1q7XE),相信结合视频再看本篇题解,更有助于大家对本题的理解**。

为了易于大家理解,我特意录制了B站视频[拿下滑动窗口! | LeetCode 209 长度最小的子数组](https://www.bilibili.com/video/BV1tZ4y1q7XE),结合视频看本题解,事半功倍!

# 思路

## 暴力解法

Expand Down
16 changes: 8 additions & 8 deletions problems/0459.重复的子字符串.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,18 @@
给定一个非空的字符串,判断它是否可以由它的一个子串重复多次构成。给定的字符串只含有小写英文字母,并且长度不超过10000。

示例 1:
输入: "abab"
输出: True
解释: 可由子字符串 "ab" 重复两次构成。
* 输入: "abab"
* 输出: True
* 解释: 可由子字符串 "ab" 重复两次构成。

示例 2:
输入: "aba"
输出: False
* 输入: "aba"
* 输出: False

示例 3:
输入: "abcabcabcabc"
输出: True
解释: 可由子字符串 "abc" 重复四次构成。 (或者子字符串 "abcabc" 重复两次构成。)
* 输入: "abcabcabcabc"
* 输出: True
* 解释: 可由子字符串 "abc" 重复四次构成。 (或者子字符串 "abcabc" 重复两次构成。)

# 思路

Expand Down
10 changes: 3 additions & 7 deletions problems/0684.冗余连接.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,8 @@
</a>
<p align="center"><strong><a href="https://mp.weixin.qq.com/s/tqCxrMEU-ajQumL1i8im9A">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!</strong></p>



# 684.冗余连接


[力扣题目链接](https://leetcode.cn/problems/redundant-connection/)

树可以看成是一个连通且 无环 的 无向 图。
Expand Down Expand Up @@ -78,7 +75,9 @@ void join(int u, int v) {
2. 将两个节点接入到同一个集合,函数:join(int u, int v),将两个节点连在同一个根节点上
3. 判断两个节点是否在同一个集合,函数:isSame(int u, int v),就是判断两个节点是不是同一个根节点

简单介绍并查集之后,我们再来看一下这道题目。
如果还不了解并查集,可以看这里:[并查集理论基础](https://programmercarl.com/图论并查集理论基础.html)

我们再来看一下这道题目。

题目说是无向图,返回一条可以删去的边,使得结果图是一个有着N个节点的树(即:只有一个根节点)。

Expand All @@ -92,7 +91,6 @@ void join(int u, int v) {

节点A 和节点 B 不在同一个集合,那么就可以将两个 节点连在一起。


(如果题目中说:如果有多个答案,则返回二维数组中最前出现的边。 那我们就要 从后向前遍历每一条边了)

如果边的两个节点已经出现在同一个集合里,说明着边的两个节点已经连在一起了,再加入这条边一定就出现环了。
Expand Down Expand Up @@ -151,8 +149,6 @@ public:

可以看出,主函数的代码很少,就判断一下边的两个节点在不在同一个集合就可以了。

这里对并查集就不展开过多的讲解了,翻到了自己十年前写过了一篇并查集的文章[并查集学习](https://blog.csdn.net/youngyangyang04/article/details/6447435),哈哈,那时候还太年轻,写不咋地,有空我会重写并查集基础篇!


# 其他语言版本

Expand Down
4 changes: 2 additions & 2 deletions problems/0685.冗余连接II.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ if (vec.size() > 0) {
}
```

在来看情况三,明确没有入度为2的情况,那么一定有有向环,找到构成环的边就是要删除的边。
在来看情况三,明确没有入度为2的情况,那么一定有向环,找到构成环的边就是要删除的边。

可以定义一个函数,代码如下:

Expand All @@ -122,7 +122,7 @@ vector<int> getRemoveEdge(const vector<vector<int>>& edges)

**因为如果两个点所在的边在添加图之前如果就可以在并查集里找到了相同的根,那么这条边添加上之后 这个图一定不是树了**

这里对并查集就不展开过多的讲解了,翻到了自己十年前写过了一篇并查集的文章[并查集学习](https://blog.csdn.net/youngyangyang04/article/details/6447435),哈哈,那时候还太年轻,写不咋地,有空我会重写一篇!
如果还不了解并查集,可以看这里:[并查集理论基础](https://programmercarl.com/图论并查集理论基础.html)

本题C++代码如下:(详细注释了)

Expand Down
Loading

AltStyle によって変換されたページ (->オリジナル) /