标签:etc splay i++ ble += open https nbsp array
滑动窗口的思想是:
1、设定前指针和后指针,先向后移动后指针直到满足条件,
2、然后向后移动前指针判断是否仍满足条件,
3、如果不满足条件继续向后移动后指针直到满足条件,找出满足条件的最短序列即可。
class Solution { public: int minSubArrayLen(int s, vector<int>& nums) { //先计算部分和 int *donser,sum=0,size=nums.size(); donser=new int[size]; for(int i=0;i<size;i++) { sum+=nums[i]; donser[i]=sum; } //滑动窗 int up=0,down=1,lable_min=size; if(size<=1) { if(size<1) return 0; else if(nums[0]>=s) return 1; else return 0; } if(donser[size-1]<s) return 0; if(donser[0]>=s) return 1; while(down<size) { int sum_now; if(up==0) sum_now=donser[down]; else sum_now=donser[down]-donser[up-1]; if(sum_now<s) { down++; continue; } if(sum_now>=s) { if(down-up+1<lable_min) lable_min=down-up+1; up++; } } return lable_min; } };
注意特值和边界
计算局部和可以简化计算
标签:etc splay i++ ble += open https nbsp array
原文地址:https://www.cnblogs.com/dzzy/p/12249440.html