长度为 K 的子数组的能量值 II (Find the Power of K-Size Subarrays II)

 

思路:单词遍历不连续都为-1

// @Title: 长度为 K 的子数组的能量值 II (Find the Power of K-Size Subarrays II)
// @Author: qisiii
// @Date: 2024-11-07 13:53:38
// @Runtime: 4 ms
// @Memory: 63.4 MB
// @comment: 单词遍历不连续都为-1
// @flag: ORANGE
class Solution {
    public int[] resultsArray(int[] nums, int k) {
        if(k==1){
            return nums;
        }
        int[] res=new int[nums.length-k+1];
        int point=0;
        int start=0;
        for(int i=0;i<nums.length&&point<res.length;i++){
            int window=i-start+1;
            if(i>0&&nums[i]!=nums[i-1]+1){
                while(i>start&&point<res.length){
                    res[point++]=-1;
                    start++;
                }
                continue;
            }
            if(window==k){
                res[point++]=nums[i];
                start++;
            }
            
        }
        return res;
    }
}
Licensed under CC BY-NC-SA 4.0
最后更新于 2024-12-27