标签:max window class ++ http res val int bsp
遍历每一个数nums[i],之后在[i, i+k]中顺序寻找最大值。
时间复杂度:O(k*n)
1 class Solution { 2 public: 3 vector<int> maxSlidingWindow(vector<int>& nums, int k) { 4 if(nums.empty()) 5 return nums; 6 7 vector<int> res; 8 for(int i = 0; i <= nums.size()-k; ++i) { 9 res.push_back(findMax(nums, i, i+k-1)); 10 } 11 12 return res; 13 } 14 15 int findMax(vector<int>& nums, int l, int r) { 16 int maxVal = nums[l]; 17 for(int i = l+1; i <= r; ++i) { 18 if(maxVal < nums[i]) 19 maxVal = nums[i]; 20 } 21 22 return maxVal; 23 } 24 };
标签:max window class ++ http res val int bsp
原文地址:https://www.cnblogs.com/FengZeng666/p/13972746.html