LeetCode 239 解答

java 复制代码
给你一个整数数组 nums,有一个大小为 k 的滑动窗口从数组的最左侧移动到数组的最右侧。你只可以看到在滑动窗口内的 k 个数字。滑动窗口每次只向右移动一位。

返回 滑动窗口中的最大值 。

示例 1:

输入:nums = [1,3,-1,-3,5,3,6,7], k = 3
输出:[3,3,5,5,6,7]
解释:
滑动窗口的位置                最大值
---------------               -----
[1  3  -1] -3  5  3  6  7       3
 1 [3  -1  -3] 5  3  6  7       3
 1  3 [-1  -3  5] 3  6  7       5
 1  3  -1 [-3  5  3] 6  7       5
 1  3  -1  -3 [5  3  6] 7       6
 1  3  -1  -3  5 [3  6  7]      7
示例 2:

输入:nums = [1], k = 1
输出:[1]
提示:

java 复制代码
eetCode 解答1:
 class Solution {
        public int[] maxSlidingWindow(int[] nums, int k) {
            int[] res = new int[nums.length - k + 1];
            LinkedList<Integer> queue = new LinkedList<>();
  		      // 如果队列不为空且当前考察元素大于等于队尾元素,则将队尾元素移除。
            // 直到,队列为空或当前考察元素小于新的队尾元素
            for (int right = 0; right < nums.length; right++) {

                while(!queue.isEmpty() && nums[queue.peekLast()] < nums[right]){
                    queue.pollLast();
                }
							 // 存储元素下标
                queue.offer(right);
							 // 计算窗口左侧边界
                int left = Math.max(0,right-k+1);
                // 当队首元素的下标小于滑动窗口左侧边界left时
                // 表示队首元素已经不再滑动窗口内,因此将其从队首移除
                while(queue.peek()<left){
                    queue.poll();
                }
			    // 由于数组下标从0开始,因此当窗口右边界right+1大于等于窗口大小k时
                // 意味着窗口形成。此时,队首元素就是该窗口内的最大值
                if(right>=k-1) {
                    res[left] = nums[queue.peek()];
                }
            }
            return res;
        }
    }

解答二,不知道为啥不通过,参考代码随想录,也是单调队列的思想

java 复制代码
  class Solution {
        public int[] maxSlidingWindow(int[] nums, int k) {
            List<Integer> ans = new ArrayList<>();

            pQueue q = new pQueue();
            for (int i = 0; i < k; i++) {
                q.push(nums[i]);
            }
            ans.add(q.front());

            for (int i = k; i < nums.length; i++) {
                q.pop(nums[i-k]);
                q.push(nums[i]);
                ans.add(q.front());
            }

            int size = ans.size();
            int[] ret = new int[size];
            for (int i = 0; i < ret.length; i++) {
                ret[i] = ans.get(i);
            }
            return ret;
        }
    }

    class pQueue {

        LinkedList<Integer> queue = new LinkedList<>();

        public void push(Integer x) {
            while (!queue.isEmpty() && queue.peekLast() < x) {
                queue.pollLast();
            }
            queue.offer(x);
        }

        public void pop(Integer x) {
            if (!queue.isEmpty() && queue.peek() == x) {
                queue.poll();
            }
        }

        public Integer front() {
            if (!queue.isEmpty()) {
                return queue.peek();
            } else {
                return null;
            }
        }
    }
相关推荐
007php0072 天前
某大厂MySQL面试之SQL注入触点发现与SQLMap测试
数据库·python·sql·mysql·面试·职场和发展·golang
JosieBook2 天前
【程序人生】有梦想就能了不起,就怕你没梦想
程序人生·职场和发展
data myth3 天前
力扣1210. 穿过迷宫的最少移动次数 详解
算法·leetcode·职场和发展
Greedy Alg3 天前
LeetCode 240. 搜索二维矩阵 II
算法·leetcode·职场和发展
墨染点香3 天前
LeetCode 刷题【68. 文本左右对齐】
算法·leetcode·职场和发展
GalaxyPokemon3 天前
LeetCode - 202. 快乐数
算法·leetcode·职场和发展
吃着火锅x唱着歌3 天前
LeetCode 522.最长特殊序列2
算法·leetcode·职场和发展
CoderYanger3 天前
MySQL数据库——3.2.1 表的增删查改-查询部分(全列+指定列+去重)
java·开发语言·数据库·mysql·面试·职场和发展
yh云想4 天前
《Java线程池面试全解析:从原理到实践的高频问题汇总》
jvm·面试·职场和发展