乐趣区

关于算法:算法滑动窗口

leetcode 239 剑指 Offer 59 – I. 滑动窗口的最大值

        const nums = [1, 3, -1, -3, 5, 3, 6, 7];
        const k = 3;
        function findMax(nums, k) {const result = []
            let temp = nums[0]
            for (let i = 0; i < nums.length; i++) {for (let j = 0; j < k; j++) {if (nums[i + j] > temp) {temp = nums[i + j]
                    }
                }
                result.push(temp)
                temp = nums[i]
                if (i == nums.length - k) {break;}
            }
            return result
        }
        const result = findMax(nums, k)
        console.log('result', result);
退出移动版