剑指offer65-67

第65题

题目描述:
在这里插入图片描述
**代码如下: **

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,0,str,flag)){
                    return true;
                }
            }
        }
        return false;
    }
    
    private boolean helper(char[] matrix, int rows, int cols, int i, int j , int k ,char[] str, int[] flag){
        //计算这个位置在数组matrix中对应的下标
        int index = i*cols + j;
        //溢出判断
        if(i>=rows || i<0 || j>=cols || j<0 || matrix[index] != str[k] || flag[index]==1){
            return false;
        }
        //如果匹配到字符数组的最后一位(经过上面的判断,此时第k位符合要求,且没有被访问)
        if(k==str.length-1){
            return true;
        }
        //没到最后一位,将当前位标志为已经访问过
        flag[index] = 1;
        //对上下左右四个位置进行探查
        if(helper(matrix, rows, cols, i - 1, j, k+1, str, flag)
                || helper(matrix, rows, cols, i + 1, j, k+1, str, flag)
                || helper(matrix, rows, cols, i, j - 1, k+1, str, flag)
                || helper(matrix, rows, cols, i, j + 1, k+1, str, flag)){
            return true;
        }
        //如果没有成功,当前路径失败
        flag[index] = 0;
        return false;
    }
    
}

第66题

题目描述:
地上有一个m行和n列的方格。一个机器人从坐标0,0的格子开始移动,每一次只能向左,右,上,下四个方向移动一格,但是不能进入行坐标和列坐标的数位之和大于k的格子。 例如,当k为18时,机器人能够进入方格(35,37),因为3+5+3+7 = 18。但是,它不能进入方格(35,38),因为3+5+3+8 = 19。请问该机器人能够达到多少个格子?

代码如下:

public class Solution {
    public int movingCount(int threshold, int rows, int cols)
    {
        //表示对应坐标已经访问过
        boolean[][] flag = new boolean[rows][cols]; 
        return helper(threshold,rows,cols,0,0,flag);
    }
    
    
    private int helper(int threshold, int rows, int cols, int i, int j, boolean[][] flag){
        if(i < 0 || i >= rows || j < 0 || j >= cols
                || flag[i][j] || bitSum(i) + bitSum(j) > threshold){
            return 0;
        }
        flag[i][j] = true;
        return helper(threshold,rows,cols,i-1,j,flag)+
            helper(threshold,rows,cols,i+1,j,flag)+
            helper(threshold,rows,cols,i,j-1,flag)+
            helper(threshold,rows,cols,i,j+1,flag)+1;
    }
    
    public int bitSum(int t){
        int count = 0;
        while (t != 0){
            count += t % 10;
            t /= 10;
        }
        return  count;
    }
}

第67题

题目描述:
给你一根长度为n的绳子,请把绳子剪成整数长的m段(m、n都是整数,n>1并且m>1),每段绳子的长度记为k[0],k[1],…,k[m]。请问k[0]xk[1]x…xk[m]可能的最大乘积是多少?例如,当绳子的长度是8时,我们把它剪成长度分别为2、3、3的三段,此时得到的最大乘积是18。

代码如下:

public class Solution {
    public int cutRope(int target) {
        //tagert<=3的情况
        if(target == 2){
            return 1;
        }
        if(target == 3){
            return 2;
        }
        
        int[] dp = new int[target+1];
        dp[1] = 1;
        dp[2] = 2;
        dp[3] = 3;
        
        //target>=4的情况
        for(int i=4;i<=target;i++){
            for(int j = 1;j<i;j++){
                dp[i] = Math.max(dp[i],dp[j]*dp[i-j]);
            }
            
        }
        return dp[target];
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值