Skip to content

Commit 6016d2c

Browse files
authored
Merge branch 'youngyangyang04:master' into master
2 parents f25f062 + 9928779 commit 6016d2c

8 files changed

+302
-68
lines changed

problems/0028.实现strStr.md

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -425,8 +425,8 @@ public:
425425
if (needle.size() == 0) {
426426
return 0;
427427
}
428-
int next[needle.size()];
429-
getNext(next, needle);
428+
vector<int> next(needle.size());
429+
getNext(&next[0], needle);
430430
int j = -1; // // 因为next数组里记录的起始位置为-1
431431
for (int i = 0; i < haystack.size(); i++) { // 注意i就从0开始
432432
while(j >= 0 && haystack[i] != needle[j + 1]) { // 不匹配
@@ -524,8 +524,8 @@ public:
524524
if (needle.size() == 0) {
525525
return 0;
526526
}
527-
int next[needle.size()];
528-
getNext(next, needle);
527+
vector<int> next(needle.size());
528+
getNext(&next[0], needle);
529529
int j = 0;
530530
for (int i = 0; i < haystack.size(); i++) {
531531
while(j > 0 && haystack[i] != needle[j]) {
@@ -1428,4 +1428,3 @@ public int[] GetNext(string needle)
14281428
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
14291429
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
14301430
</a>
1431-

problems/0045.跳跃游戏II.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,34 @@ class Solution:
285285

286286
### Go
287287

288+
289+
```go
290+
/**
291+
* @date: 2024 Jan 06
292+
* @time: 13:44
293+
* @author: Chris
294+
**/
295+
// 贪心算法优化版
296+
297+
// 记录步骤规则:每超过上一次可达最大范围,需要跳跃一次,次数+1
298+
// 记录位置:i == lastDistance + 1
299+
func jump(nums []int) int {
300+
// 根据题目规则,初始位置为nums[0]
301+
lastDistance := 0 // 上一次覆盖范围
302+
curDistance := 0 // 当前覆盖范围(可达最大范围)
303+
minStep := 0 // 记录最少跳跃次数
304+
305+
for i := 0; i < len(nums); i++ {
306+
if i == lastDistance+1 { // 在上一次可达范围+1的位置,记录步骤
307+
minStep++ // 跳跃次数+1
308+
lastDistance = curDistance // 记录时才可以更新
309+
}
310+
curDistance = max(nums[i]+i, curDistance) // 更新当前可达的最大范围
311+
}
312+
return minStep
313+
}
314+
```
315+
288316
```go
289317
// 贪心版本一
290318
func jump(nums []int) int {

problems/0116.填充每个节点的下一个右侧节点指针.md

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -358,7 +358,84 @@ function connect(root: NodePro | null): NodePro | null {
358358
};
359359
```
360360

361+
```csharp
362+
//递归
363+
public class Solution {
364+
public Node Connect(Node root) {
365+
if (root == null) {
366+
return null;
367+
}
368+
369+
ConnectNodes(root.left, root.right);
370+
371+
return root;
372+
}
373+
374+
private void ConnectNodes(Node node1, Node node2) {
375+
if (node1 == null || node2 == null) {
376+
return;
377+
}
378+
379+
// 将左子节点的 next 指向右子节点
380+
node1.next = node2;
381+
382+
// 递归连接当前节点的左右子节点
383+
ConnectNodes(node1.left, node1.right);
384+
ConnectNodes(node2.left, node2.right);
385+
386+
// 连接跨越父节点的两个子树
387+
ConnectNodes(node1.right, node2.left);
388+
}
389+
}
390+
391+
392+
// 迭代
393+
public class Solution
394+
{
395+
public Node Connect(Node root)
396+
{
397+
Queue<Node> que = new Queue<Node>();
398+
399+
if (root != null)
400+
{
401+
que.Enqueue(root);
402+
}
361403

404+
while (que.Count > 0)
405+
{
406+
407+
var queSize = que.Count;
408+
for (int i = 0; i < queSize; i++)
409+
{
410+
var cur = que.Dequeue();
411+
412+
// 当这个节点不是这一层的最后的节点
413+
if (i != queSize - 1)
414+
{
415+
// 当前节点指向下一个节点
416+
cur.next = que.Peek();
417+
}
418+
// 否则指向空
419+
else
420+
{
421+
cur.next = null;
422+
}
423+
424+
if (cur.left != null)
425+
{
426+
que.Enqueue(cur.left);
427+
}
428+
if (cur.right != null)
429+
{
430+
que.Enqueue(cur.right);
431+
}
432+
}
433+
}
434+
435+
return root;
436+
}
437+
}
438+
```
362439

363440

364441
<p align="center">

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

Lines changed: 31 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -389,50 +389,41 @@ function numIslands(grid: string[][]): number {
389389
### Go
390390

391391
```go
392+
393+
var DIRECTIONS = [4][2]int{{-1, 0}, {0, -1}, {1, 0}, {0, 1}}
394+
392395
func numIslands(grid [][]byte) int {
393-
// 用1标记已访问
394-
visited := make([][]int, len(grid))
395-
for i := 0; i < len(visited); i++{
396-
visited[i] = make([]int, len(grid[0]))
397-
}
396+
res := 0
398397

399-
var bfs func(x, y int)
400-
bfs = func(x, y int){
401-
stack := make([][]int, 0)
402-
stack = append(stack, []int{x, y})
403-
moveX := []int{1, -1, 0, 0}
404-
moveY := []int{0, 0, 1, -1}
405-
406-
for len(stack) != 0{
407-
node := stack[len(stack) - 1]
408-
stack = stack[:len(stack) - 1]
409-
410-
for i := 0; i < 4; i++{
411-
dx := moveX[i] + node[0]
412-
dy := moveY[i] + node[1]
413-
if dx < 0 || dx >= len(grid) || dy < 0 || dy >= len(grid[0]) || visited[dx][dy] == 1{
414-
continue
415-
}
416-
visited[dx][dy] = 1
417-
if grid[dx][dy] == '1'{
418-
stack = append(stack, []int{dx,dy})
419-
}
420-
}
421-
}
422-
}
398+
visited := make([][]bool, len(grid))
399+
for i := 0; i < len(grid); i++ {
400+
visited[i] = make([]bool, len(grid[0]))
401+
}
423402

424-
result := 0
425-
for i := 0; i < len(grid); i++{
426-
for j := 0; j < len(grid[0]); j++{
427-
if visited[i][j] == 0 && grid[i][j] == '1'{
428-
bfs(i, j)
429-
visited[i][j] = 1
430-
result++
431-
}
432-
}
433-
}
403+
for i, rows := range grid {
404+
for j, v := range rows {
405+
if v == '1' && !visited[i][j] {
406+
res++
407+
dfs(grid, visited, i, j)
408+
}
409+
}
410+
}
411+
412+
return res
413+
}
414+
415+
func dfs(grid [][]byte, visited [][]bool, i, j int) {
416+
visited[x][y] = true
417+
for _, d := range DIRECTIONS {
418+
x, y := i+d[0], j+d[1]
419+
if x < 0 || x >= len(grid) || y < 0 || y >= len(grid[0]) {
420+
continue
421+
}
422+
if grid[x][y] == '1' && !visited[x][y] {
423+
dfs(grid, visited, x, y)
424+
}
425+
}
434426

435-
return result
436427
}
437428
```
438429

problems/0225.用队列实现栈.md

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1046,6 +1046,8 @@ class MyStack() {
10461046

10471047
### C#:
10481048

1049+
> 双队列
1050+
10491051
```csharp
10501052
public class MyStack {
10511053
Queue<int> queue1;
@@ -1080,6 +1082,54 @@ public class MyStack {
10801082
}
10811083
```
10821084

1085+
> 单队列
1086+
1087+
```c#
1088+
/*
1089+
* @lc app=leetcode id=225 lang=csharp
1090+
* 版本二:单队列
1091+
* [225] Implement Stack using Queues
1092+
*/
1093+
1094+
// @lc code=start
1095+
public class MyStack {
1096+
Queue<int> myQueue;
1097+
public MyStack() {
1098+
myQueue = new Queue<int>();
1099+
}
1100+
1101+
public void Push(int x) {
1102+
myQueue.Enqueue(x);
1103+
}
1104+
1105+
//使用一个队列实现
1106+
public int Pop() {
1107+
//一个队列在模拟栈弹出元素的时候只要将队列头部的元素(除了最后一个元素外) 重新添加到队列尾部,此时再去弹出元素就是栈的顺序了。
1108+
for (var i = 0; i < myQueue.Count-1; i++)
1109+
{
1110+
myQueue.Enqueue(myQueue.Dequeue());
1111+
}
1112+
return myQueue.Dequeue();
1113+
}
1114+
1115+
//复用Pop()的代码
1116+
public int Top() {
1117+
int res = Pop();
1118+
myQueue.Enqueue(res);
1119+
return res;
1120+
}
1121+
1122+
public bool Empty() {
1123+
return (myQueue.Count == 0);
1124+
}
1125+
}
1126+
1127+
// @lc code=end
1128+
1129+
```
1130+
1131+
1132+
10831133
### PHP:
10841134

10851135
> 双队列
@@ -1203,3 +1253,4 @@ impl MyStack {
12031253
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
12041254
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
12051255
</a>
1256+

0 commit comments

Comments
 (0)