auto commmit
This commit is contained in:
parent
a5697cd803
commit
e3793d43ea
@ -34,7 +34,7 @@
|
|||||||
|
|
||||||
<div align="center"> <img src="https://cs-notes-1256109796.cos.ap-guangzhou.myqcloud.com/95903878-725b-4ed9-bded-bc4aae0792a9.jpg"/> </div><br>
|
<div align="center"> <img src="https://cs-notes-1256109796.cos.ap-guangzhou.myqcloud.com/95903878-725b-4ed9-bded-bc4aae0792a9.jpg"/> </div><br>
|
||||||
|
|
||||||
广度优先搜索一层一层地进行遍历,每层遍历都以上一层遍历的结果作为起点,遍历一个距离能访问到的所有节点。需要注意的是,遍历过的节点不能再次被遍历。
|
广度优先搜索一层一层地进行遍历,每层遍历都是以上一层遍历的结果作为起点,遍历一个距离能访问到的所有节点。需要注意的是,遍历过的节点不能再次被遍历。
|
||||||
|
|
||||||
第一层:
|
第一层:
|
||||||
|
|
||||||
@ -75,7 +75,10 @@
|
|||||||
题目描述:0 表示可以经过某个位置,求解从左上角到右下角的最短路径长度。
|
题目描述:0 表示可以经过某个位置,求解从左上角到右下角的最短路径长度。
|
||||||
|
|
||||||
```java
|
```java
|
||||||
public int shortestPathBinaryMatrix(int[][] grids) {
|
public int shortestPathBinaryMatrix(int[][] grids) {
|
||||||
|
if (grids == null || grids.length == 0 || grids[0].length == 0) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
int[][] direction = {{1, -1}, {1, 0}, {1, 1}, {0, -1}, {0, 1}, {-1, -1}, {-1, 0}, {-1, 1}};
|
int[][] direction = {{1, -1}, {1, 0}, {1, 1}, {0, -1}, {0, 1}, {-1, -1}, {-1, 0}, {-1, 1}};
|
||||||
int m = grids.length, n = grids[0].length;
|
int m = grids.length, n = grids[0].length;
|
||||||
Queue<Pair<Integer, Integer>> queue = new LinkedList<>();
|
Queue<Pair<Integer, Integer>> queue = new LinkedList<>();
|
||||||
@ -87,21 +90,24 @@ public int shortestPathBinaryMatrix(int[][] grids) {
|
|||||||
while (size-- > 0) {
|
while (size-- > 0) {
|
||||||
Pair<Integer, Integer> cur = queue.poll();
|
Pair<Integer, Integer> cur = queue.poll();
|
||||||
int cr = cur.getKey(), cc = cur.getValue();
|
int cr = cur.getKey(), cc = cur.getValue();
|
||||||
|
if (grids[cr][cc] == 1) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (cr == m - 1 && cc == n - 1) {
|
||||||
|
return pathLength;
|
||||||
|
}
|
||||||
grids[cr][cc] = 1; // 标记
|
grids[cr][cc] = 1; // 标记
|
||||||
for (int[] d : direction) {
|
for (int[] d : direction) {
|
||||||
int nr = cr + d[0], nc = cc + d[1];
|
int nr = cr + d[0], nc = cc + d[1];
|
||||||
if (nr < 0 || nr >= m || nc < 0 || nc >= n || grids[nr][nc] == 1) {
|
if (nr < 0 || nr >= m || nc < 0 || nc >= n) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
if (nr == m - 1 && nc == n - 1) {
|
|
||||||
return pathLength + 1;
|
|
||||||
}
|
|
||||||
queue.add(new Pair<>(nr, nc));
|
queue.add(new Pair<>(nr, nc));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
## 2. 组成整数的最小平方数数量
|
## 2. 组成整数的最小平方数数量
|
||||||
|
@ -34,7 +34,7 @@
|
|||||||
|
|
||||||
<div align="center"> <img src="https://cs-notes-1256109796.cos.ap-guangzhou.myqcloud.com/95903878-725b-4ed9-bded-bc4aae0792a9.jpg"/> </div><br>
|
<div align="center"> <img src="https://cs-notes-1256109796.cos.ap-guangzhou.myqcloud.com/95903878-725b-4ed9-bded-bc4aae0792a9.jpg"/> </div><br>
|
||||||
|
|
||||||
广度优先搜索一层一层地进行遍历,每层遍历都以上一层遍历的结果作为起点,遍历一个距离能访问到的所有节点。需要注意的是,遍历过的节点不能再次被遍历。
|
广度优先搜索一层一层地进行遍历,每层遍历都是以上一层遍历的结果作为起点,遍历一个距离能访问到的所有节点。需要注意的是,遍历过的节点不能再次被遍历。
|
||||||
|
|
||||||
第一层:
|
第一层:
|
||||||
|
|
||||||
@ -75,7 +75,10 @@
|
|||||||
题目描述:0 表示可以经过某个位置,求解从左上角到右下角的最短路径长度。
|
题目描述:0 表示可以经过某个位置,求解从左上角到右下角的最短路径长度。
|
||||||
|
|
||||||
```java
|
```java
|
||||||
public int shortestPathBinaryMatrix(int[][] grids) {
|
public int shortestPathBinaryMatrix(int[][] grids) {
|
||||||
|
if (grids == null || grids.length == 0 || grids[0].length == 0) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
int[][] direction = {{1, -1}, {1, 0}, {1, 1}, {0, -1}, {0, 1}, {-1, -1}, {-1, 0}, {-1, 1}};
|
int[][] direction = {{1, -1}, {1, 0}, {1, 1}, {0, -1}, {0, 1}, {-1, -1}, {-1, 0}, {-1, 1}};
|
||||||
int m = grids.length, n = grids[0].length;
|
int m = grids.length, n = grids[0].length;
|
||||||
Queue<Pair<Integer, Integer>> queue = new LinkedList<>();
|
Queue<Pair<Integer, Integer>> queue = new LinkedList<>();
|
||||||
@ -87,21 +90,24 @@ public int shortestPathBinaryMatrix(int[][] grids) {
|
|||||||
while (size-- > 0) {
|
while (size-- > 0) {
|
||||||
Pair<Integer, Integer> cur = queue.poll();
|
Pair<Integer, Integer> cur = queue.poll();
|
||||||
int cr = cur.getKey(), cc = cur.getValue();
|
int cr = cur.getKey(), cc = cur.getValue();
|
||||||
|
if (grids[cr][cc] == 1) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (cr == m - 1 && cc == n - 1) {
|
||||||
|
return pathLength;
|
||||||
|
}
|
||||||
grids[cr][cc] = 1; // 标记
|
grids[cr][cc] = 1; // 标记
|
||||||
for (int[] d : direction) {
|
for (int[] d : direction) {
|
||||||
int nr = cr + d[0], nc = cc + d[1];
|
int nr = cr + d[0], nc = cc + d[1];
|
||||||
if (nr < 0 || nr >= m || nc < 0 || nc >= n || grids[nr][nc] == 1) {
|
if (nr < 0 || nr >= m || nc < 0 || nc >= n) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
if (nr == m - 1 && nc == n - 1) {
|
|
||||||
return pathLength + 1;
|
|
||||||
}
|
|
||||||
queue.add(new Pair<>(nr, nc));
|
queue.add(new Pair<>(nr, nc));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
## 2. 组成整数的最小平方数数量
|
## 2. 组成整数的最小平方数数量
|
||||||
|
Loading…
x
Reference in New Issue
Block a user