剑指编程(13)

本文探讨了滑动窗口算法的应用,通过实例讲解如何找到数组中特定窗口的最大值,并介绍了一个矩阵路径搜索问题,旨在寻找包含指定字符串的路径。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、
给定一个数组和滑动窗口的大小,找出所有滑动窗口里数值的最大值。
例如,如果输入数组{2,3,4,2,6,2,5,1}及滑动窗口的大小3
那么一共存在6个滑动窗口,他们的最大值分别为{4,4,6,6,6,5};
针对数组{2,3,4,2,6,2,5,1}的滑动窗口有以下6个:
{[2,3,4],2,6,2,5,1}, {2,[3,4,2],6,2,5,1},
{2,3,[4,2,6],2,5,1}, {2,3,4,[2,6,2],5,1},
{2,3,4,2,[6,2,5],1}, {2,3,4,2,6,[2,5,1]}。

public class Solution {
    //上一次得到的最大值可以和这次遍历的数相比
    public ArrayList<Integer> maxInWindows(int [] num, int size) {
        ArrayList<Integer> res = new ArrayList<>();
        if(num == null || num.length == 0 || size > num.length || size == 0) {
            return res;
        }
        //pos用于存放最大值的位置
        int max = 0, pos = 0;
        for(int i = 0; i < size; i++) {
            if(num[i] > max) {
                max = num[i];
                pos = i;
            }
        }
        res.add(max);
        for(int i = size; i < num.length; i++) {
            if(pos + size > i) {
                if(max < num[i]) {
                    max = num[i];
                    pos = i;
                }
                res.add(max);
            }else {
                //当最大值不在窗口后,重新统计最大值
                max = 0;
                for(int j = i; j > i - size && j >= 0; j--) {
                    if(num[j] > max) {
                        max = num[j];
                        pos = j;
                    }
                }
                res.add(max);
            }
        }
        return res;
    }
}

二、
请设计一个函数,用来判断在一个矩阵中是否存在一条包含某字符串所有字符的路径。
路径可以从矩阵中的任意一个格子开始,每一步可以在矩阵中向左,向右,向上,向下移动一个格子。
如果一条路径经过了矩阵中的某一个格子,则该路径不能再进入该格子。
例如
a b c e
s f c s
a d e e
矩阵中包含一条字符串”bcced”的路径,但是矩阵中不包含”abcb”路径

public class Solution {
    public boolean hasPath(char[] matrix, int rows, int cols, char[] str) {
        if(matrix == null || matrix.length == 0 ||
                str == null || str.length == 0 ||
                rows <= 0 || cols <= 0 ||
                matrix.length != rows * cols || rows * cols < str.length) {
            return false;
        }
        boolean[] vis = new boolean[rows * cols];
        //每个点都有可能是起点
        for(int i = 0; i < rows; i++) {
            for(int j = 0; j < cols; j++) {
                if(hasPath(matrix, rows, cols, str, i, j, vis, 0)) {
                    return true;
                }
            }
        }
        return false;
    }

    public boolean hasPath(char[] matrix, int rows, int cols, char[] str, int x, int y, boolean[] vis, int pos) {
        int index = x * cols + y;
        //出界
        if(x < 0 || x >= rows || y < 0 || y >= cols) {
            return false;
        }
        //访问过
        if(vis[index]) {
            return false;
        }
        //和字符串不匹配
        if(matrix[index] != str[pos]) {
            return false;
        }
        //结束条件
        if(pos == str.length - 1) {
            return true;
        }
        vis[index] = true;
        //------------开始深入
        if(hasPath(matrix, rows, cols, str, x - 1, y, vis, pos+ 1) ||
                hasPath(matrix, rows, cols, str, x + 1, y, vis, pos+ 1) ||
                hasPath(matrix, rows, cols, str, x, y - 1, vis, pos+ 1) ||
                hasPath(matrix, rows, cols, str, x, y + 1, vis, pos+ 1)) {
            return true;
        }
        //------------回溯
        vis[index] = false;
        return false;
    }
}

三、
地上有一个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[][] vis = new boolean[rows][cols];
        return dfs(vis, threshold, rows, cols, 0, 0);
    }

    public int dfs(boolean[][] vis, int threshold, int rows, int cols, int x, int y) {
        //出界不能到达
        if(x < 0 || y < 0 || x >= rows || y >= cols) {
            return 0;
        }
        //访问过不再算
        if(vis[x][y]) {
            return 0;
        }
        //不能进入的不算
        if(illegal(threshold, x, y)) {
            return 0;
        }
        //自身这个点可以算
        int res = 1;
        //访问过的点无需再回溯了,已经统计进来了
        vis[x][y] = true;
        res += dfs(vis, threshold, rows, cols, x - 1, y);
        res += dfs(vis, threshold, rows, cols, x + 1, y);
        res += dfs(vis, threshold, rows, cols, x, y - 1);
        res += dfs(vis, threshold, rows, cols, x, y + 1);
        return res;
    }

    public boolean illegal(int threshold, int x, int y) {
        int sum = 0;
        while(x != 0) {
            sum += x % 10;
            x /= 10;
        }
        while(y != 0) {
            sum += y % 10;
            y /= 10;
        }
        if(sum > threshold) {
            return true;
        }else {
            return false;
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值