剑指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
原创粉丝点击