剑指offer面试题 java解答66-67
来源:互联网 发布:购物系统源码 编辑:程序博客网 时间:2024/06/04 23:28
面试题66:矩阵中的路径
public class Test66 { public static boolean hasPath(char[][] matrix,char[] str){ if (matrix==null||matrix.length<1&&matrix[0].length<1) { return false; } int rows=matrix.length; int cols=matrix[0].length; boolean[] visited=new boolean[rows*cols]; int pathLength=0; for (int row = 0; row < rows; row++) { for (int col = 0; col < cols; col++) { if (hasPathCore(matrix,rows,cols,row,col,str,pathLength,visited)) { return true; } } } return false; } private static boolean hasPathCore(char[][] matrix,int rows, int cols, int row, int col, char[] str, int pathLength, boolean[] visited) { if (pathLength==str.length) { return true; } boolean hasPath=false; if (row>=0&&row<rows&&col>=0&&col<cols&&!visited[row*cols+col]&&matrix[row][col]==str[pathLength]) { pathLength++; hasPath=hasPathCore(matrix, rows, cols, row-1, col, str, pathLength, visited) ||hasPathCore(matrix, rows, cols, row+1, col, str, pathLength, visited) ||hasPathCore(matrix, rows, cols, row, col-1, str, pathLength, visited) ||hasPathCore(matrix, rows, cols, row, col+1, str, pathLength, visited); if (!hasPath) { pathLength--; visited[row*cols+col]=false; } } return hasPath; } public static void main(String[] args) { char[][] matrix={ {'a','b','c','e'}, {'s','f','c','s'}, {'a','d','e','e'}, }; char[] str={'b','c','c','e','d'}; boolean hasPath=Test66.hasPath(matrix, str); System.out.println(hasPath); }}
面试题67:机器人的运动范围
public class Test67 { public static int movingCount(int threshold,int rows,int cols){ if (threshold<0||rows*cols<1) { return 0; } boolean[] visited=new boolean[rows*cols]; int count=movingCountCore(threshold,rows,cols,0,0,visited); return count; } private static int movingCountCore(int threshold, int rows, int cols, int row,int col, boolean[] visited) { int count=0; if (check(threshold, rows, cols, row, col, visited)) { visited[row*cols+col]=true; count=1+movingCountCore(threshold, rows, cols, row-1, col, visited) +movingCountCore(threshold, rows, cols, row+1, col, visited) +movingCountCore(threshold, rows, cols, row, col-1, visited) +movingCountCore(threshold, rows, cols, row, col+1, visited); } return count; } private static boolean check(int threshold, int rows, int cols, int row, int col,boolean[] visited) { if (row>=0&&row<rows&&col>=0&&col<cols&&!visited[row*cols+col]&&getDigitSum(row)+getDigitSum(col)<=threshold) { return true; } return false; } private static int getDigitSum(int number) { int sum=0; while (number>0) { sum+=number%10; number/=10; } return sum; } public static void main(String[] args) { int count=Test67.movingCount(5, 4, 4); System.out.println(count); }}
0 0
- 剑指offer面试题 java解答66-67
- 剑指offer面试题 java解答41-45
- 剑指offer面试题 java解答46-50
- 剑指offer面试题 java解答36-40
- 剑指offer面试题 java解答31-35
- 剑指offer面试题 java解答51-55
- 剑指offer面试题 java解答56-60
- 剑指offer面试题 java解答26-30
- 剑指offer面试题 java解答21-25
- 剑指offer面试题 java解答16-20
- 剑指offer面试题 java解答11-15
- 剑指offer面试题 java解答6-10
- 剑指offer面试题 java解答1-5
- 剑指offer面试题 java解答61-65
- JAVA面试题及解答
- JAVA面试题及解答
- java经典面试题解答
- 剑指Offer面试题66:矩阵中的路径 java实现
- 深入Java关键字null
- youcompleteme为nodejs安装tern-completer项目根目录需要.tern-project文件
- css的一些属性值
- 获取java文件路径
- 给MFC对话框加背景图
- 剑指offer面试题 java解答66-67
- C# :this的用法(冒号后面的this)
- LRU算法实现
- [Html&JS] 一个网页上,不同的子父窗口iframe之间如何相互调用
- 318. Maximum Product of Word Lengths
- mysql查询今天,昨天,近7天,近30天,本月,上一月数据方法
- 技术债务:究竟让你付出了多大代价?
- springAOP事物管理不生效原因总结
- C/C++面试题(2)——查找链表中倒数第k个数