diff --git a/docs/notes/Leetcode 题解 - 搜索.md b/docs/notes/Leetcode 题解 - 搜索.md index 9f14efbd..0e245d4c 100644 --- a/docs/notes/Leetcode 题解 - 搜索.md +++ b/docs/notes/Leetcode 题解 - 搜索.md @@ -34,7 +34,7 @@

-广度优先搜索一层一层地进行遍历,每层遍历都以上一层遍历的结果作为起点,遍历一个距离能访问到的所有节点。需要注意的是,遍历过的节点不能再次被遍历。 +广度优先搜索一层一层地进行遍历,每层遍历都是以上一层遍历的结果作为起点,遍历一个距离能访问到的所有节点。需要注意的是,遍历过的节点不能再次被遍历。 第一层: @@ -75,33 +75,39 @@ 题目描述:0 表示可以经过某个位置,求解从左上角到右下角的最短路径长度。 ```java -public int shortestPathBinaryMatrix(int[][] grids) { - 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; - Queue> queue = new LinkedList<>(); - queue.add(new Pair<>(0, 0)); - int pathLength = 0; - while (!queue.isEmpty()) { - int size = queue.size(); - pathLength++; - while (size-- > 0) { - Pair cur = queue.poll(); - int cr = cur.getKey(), cc = cur.getValue(); - grids[cr][cc] = 1; // 标记 - for (int[] d : direction) { - int nr = cr + d[0], nc = cc + d[1]; - if (nr < 0 || nr >= m || nc < 0 || nc >= n || grids[nr][nc] == 1) { + 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 m = grids.length, n = grids[0].length; + Queue> queue = new LinkedList<>(); + queue.add(new Pair<>(0, 0)); + int pathLength = 0; + while (!queue.isEmpty()) { + int size = queue.size(); + pathLength++; + while (size-- > 0) { + Pair cur = queue.poll(); + int cr = cur.getKey(), cc = cur.getValue(); + if (grids[cr][cc] == 1) { continue; } - if (nr == m - 1 && nc == n - 1) { - return pathLength + 1; + if (cr == m - 1 && cc == n - 1) { + return pathLength; + } + grids[cr][cc] = 1; // 标记 + for (int[] d : direction) { + int nr = cr + d[0], nc = cc + d[1]; + if (nr < 0 || nr >= m || nc < 0 || nc >= n) { + continue; + } + queue.add(new Pair<>(nr, nc)); } - queue.add(new Pair<>(nr, nc)); } } + return -1; } - return -1; -} ``` ## 2. 组成整数的最小平方数数量 diff --git a/notes/Leetcode 题解 - 搜索.md b/notes/Leetcode 题解 - 搜索.md index 9f14efbd..0e245d4c 100644 --- a/notes/Leetcode 题解 - 搜索.md +++ b/notes/Leetcode 题解 - 搜索.md @@ -34,7 +34,7 @@

-广度优先搜索一层一层地进行遍历,每层遍历都以上一层遍历的结果作为起点,遍历一个距离能访问到的所有节点。需要注意的是,遍历过的节点不能再次被遍历。 +广度优先搜索一层一层地进行遍历,每层遍历都是以上一层遍历的结果作为起点,遍历一个距离能访问到的所有节点。需要注意的是,遍历过的节点不能再次被遍历。 第一层: @@ -75,33 +75,39 @@ 题目描述:0 表示可以经过某个位置,求解从左上角到右下角的最短路径长度。 ```java -public int shortestPathBinaryMatrix(int[][] grids) { - 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; - Queue> queue = new LinkedList<>(); - queue.add(new Pair<>(0, 0)); - int pathLength = 0; - while (!queue.isEmpty()) { - int size = queue.size(); - pathLength++; - while (size-- > 0) { - Pair cur = queue.poll(); - int cr = cur.getKey(), cc = cur.getValue(); - grids[cr][cc] = 1; // 标记 - for (int[] d : direction) { - int nr = cr + d[0], nc = cc + d[1]; - if (nr < 0 || nr >= m || nc < 0 || nc >= n || grids[nr][nc] == 1) { + 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 m = grids.length, n = grids[0].length; + Queue> queue = new LinkedList<>(); + queue.add(new Pair<>(0, 0)); + int pathLength = 0; + while (!queue.isEmpty()) { + int size = queue.size(); + pathLength++; + while (size-- > 0) { + Pair cur = queue.poll(); + int cr = cur.getKey(), cc = cur.getValue(); + if (grids[cr][cc] == 1) { continue; } - if (nr == m - 1 && nc == n - 1) { - return pathLength + 1; + if (cr == m - 1 && cc == n - 1) { + return pathLength; + } + grids[cr][cc] = 1; // 标记 + for (int[] d : direction) { + int nr = cr + d[0], nc = cc + d[1]; + if (nr < 0 || nr >= m || nc < 0 || nc >= n) { + continue; + } + queue.add(new Pair<>(nr, nc)); } - queue.add(new Pair<>(nr, nc)); } } + return -1; } - return -1; -} ``` ## 2. 组成整数的最小平方数数量