diff --git a/notes/剑指 offer 题解.md b/notes/剑指 offer 题解.md index 6af4e970..c5fdda86 100644 --- a/notes/剑指 offer 题解.md +++ b/notes/剑指 offer 题解.md @@ -425,36 +425,35 @@ public int minNumberInRotateArray(int[] array) { **ÌâÄ¿ÃèÊö** -ÇëÉè¼ÆÒ»¸öº¯Êý£¬ÓÃÀ´ÅжÏÔÚÒ»¸ö¾ØÕóÖÐÊÇ·ñ´æÔÚÒ»Ìõ°üº¬Ä³×Ö·û´®ËùÓÐ×Ö·ûµÄ·¾¶¡£Â·¾¶¿ÉÒÔ´Ó¾ØÕóÖеÄÈÎÒâÒ»¸ö¸ñ×Ó¿ªÊ¼£¬Ã¿Ò»²½¿ÉÒÔÔÚ¾ØÕóÖÐÏò×ó£¬ÏòÓÒ£¬ÏòÉÏ£¬ÏòÏÂÒƶ¯Ò»¸ö¸ñ×Ó¡£Èç¹ûÒ»Ìõ·¾¶¾­¹ýÁ˾ØÕóÖеÄijһ¸ö¸ñ×Ó£¬Ôò¸Ã·¾¶²»ÄÜÔÙ½øÈë¸Ã¸ñ×Ó¡£ ÀýÈç a b c e s f c s a d e e ¾ØÕóÖаüº¬Ò»Ìõ×Ö·û´® "bcced" µÄ·¾¶£¬µ«ÊǾØÕóÖв»°üº¬ "abcb" ·¾¶£¬ÒòΪ×Ö·û´®µÄµÚÒ»¸ö×Ö·û b Õ¼¾ÝÁ˾ØÕóÖеĵÚÒ»Ðеڶþ¸ö¸ñ×ÓÖ®ºó£¬Â·¾¶²»ÄÜÔٴνøÈë¸Ã¸ñ×Ó¡£ +ÇëÉè¼ÆÒ»¸öº¯Êý£¬ÓÃÀ´ÅжÏÔÚÒ»¸ö¾ØÕóÖÐÊÇ·ñ´æÔÚÒ»Ìõ°üº¬Ä³×Ö·û´®ËùÓÐ×Ö·ûµÄ·¾¶¡£Â·¾¶¿ÉÒÔ´Ó¾ØÕóÖеÄÈÎÒâÒ»¸ö¸ñ×Ó¿ªÊ¼£¬Ã¿Ò»²½¿ÉÒÔÔÚ¾ØÕóÖÐÏò×ó£¬ÏòÓÒ£¬ÏòÉÏ£¬ÏòÏÂÒƶ¯Ò»¸ö¸ñ×Ó¡£Èç¹ûÒ»Ìõ·¾¶¾­¹ýÁ˾ØÕóÖеÄijһ¸ö¸ñ×Ó£¬Ôò¸Ã·¾¶²»ÄÜÔÙ½øÈë¸Ã¸ñ×Ó¡£ÀýÈç a b c e s f c s a d e e ¾ØÕóÖаüº¬Ò»Ìõ×Ö·û´® "bcced" µÄ·¾¶£¬µ«ÊǾØÕóÖв»°üº¬ "abcb" ·¾¶£¬ÒòΪ×Ö·û´®µÄµÚÒ»¸ö×Ö·û b Õ¼¾ÝÁ˾ØÕóÖеĵÚÒ»Ðеڶþ¸ö¸ñ×ÓÖ®ºó£¬Â·¾¶²»ÄÜÔٴνøÈë¸Ã¸ñ×Ó¡£ ```java private int[][] next = {{0, -1}, {0, 1}, {-1, 0}, {1, 0}}; - -public boolean hasPath(char[] matrix, int rows, int cols, char[] str) { - if (rows == 0 || cols == 0) return false; +public boolean hasPath(char[] matrix, int rows, int cols, char[] str){ + if(rows == 0 || cols == 0) return false; char[][] m = new char[rows][cols]; - for (int i = 0, idx = 0; i < rows; i++) { - for (int j = 0; j < cols; j++) { + for(int i = 0, idx = 0; i < rows; i++) { + for(int j = 0; j < cols; j++) { m[i][j] = matrix[idx++]; } } - for (int i = 0; i < rows; i++) { - for (int j = 0; j < cols; j++) { - if (backtracking(m, rows, cols, str, new boolean[rows][cols], 0, i, j)) return true; + for(int i = 0; i < rows; i++) { + for(int j = 0; j < cols; j ++) { + if(backtracking(m, rows, cols, str, new boolean[rows][cols], 0, i, j)) return true; } } return false; } private boolean backtracking(char[][] m, int rows, int cols, char[] str, boolean[][] used, int path, int r, int c) { - if (path == str.length) return true; - if (r < 0 || r >= rows || c < 0 || c >= cols) return false; - if (m[r][c] != str[path]) return false; - if (used[r][c]) return false; + if(path == str.length) return true; + if(r < 0 || r >= rows || c < 0 || c >= cols) return false; + if(m[r][c] != str[path]) return false; + if(used[r][c]) return false; used[r][c] = true; - for (int i = 0; i < next.length; i++) { - if (backtracking(m, rows, cols, str, used, path + 1, r + next[i][0], c + next[i][1])) return true; + for(int i = 0; i < next.length; i++) { + if(backtracking(m, rows, cols, str, used, path + 1, r + next[i][0], c + next[i][1])) return true; } used[r][c] = false; return false;