标签:item pat turn dex OLE help ret 访问 abc
/** 用一个状态数组保存之前访问过的字符,然后再分别按上,下,左,右递归 */ public class Solution { public boolean hasPath(char[] matrix, int rows, int cols, char[] str) { int[] flag = new int[matrix.length]; for(int i = 0; i < rows; i++) { for(int j = 0; j < cols; j++) { if(helper(matrix, rows, cols, i, j, str, 0, flag)) return true; } } return false; } public boolean helper(char[] matrix, int rows, int cols, int i, int j, char[] str, int k, int[] flag) { int index = i * cols + j; if(i<0||i>=rows||j<0||j>=cols||matrix[index]!=str[k]||flag[index]==1) return false; if(k == str.length-1) return true; flag[index] = 1; if(helper(matrix, rows, cols, i-1, j, str, k+1, flag)|| helper(matrix, rows, cols, i+1, j, str, k+1, flag)|| helper(matrix, rows, cols, i, j-1, str, k+1, flag)|| helper(matrix, rows, cols, i, j+1, str, k+1, flag)){ return true; } flag[index] = 0; return false; } }
标签:item pat turn dex OLE help ret 访问 abc
原文地址:https://www.cnblogs.com/yihangZhou/p/10544842.html