我的js算法爬坑之旅-滑动窗口的最大值

105 阅读1分钟

第八十二天:剑指 Offer 59 - I,滑动窗口的最大值

地址:leetcode-cn.com/problems/hu…

思路:for循环,内嵌套for循环,用Math.max来获取最大值

var maxSlidingWindow = function(nums, k) {
  let res = [];
  if(k === 0)
  {
    return res;
  }
  for(let i = 0 ; i <= nums.length - k; i++)
  {
    let max = nums[i]
    for(let j = i + 1; j < k + i; j++)
    {
      max = Math.max(max,nums[j]);
    }
    res.push(max);
  }
  return res;
};

执行用时:136 ms, 在所有 JavaScript 提交中击败了69.75%的用户

内存消耗:43.3 MB, 在所有 JavaScript 提交中击败了95.09%的用户