别人的代码
class Solution { public: int minSubArrayLen(int s, vector<int>& nums) { int l, r, cum, res = nums.size()+1; l = r = cum = 0; while ((unsigned int)r < nums.size()) { cum += nums[r++]; while (cum >= s) { res = min(res, r-l); cum -= nums[l++]; } } return res<=nums.size()?res:0; } };
我的 280ms
class Solution { public: int minSubArrayLen(int s, vector<int>& nums) { vector<int> res; int start=0,end=0,len=INT_MAX; for(int end=0;end<nums.size();++end) { while(sum(nums,s,start,end) && start<=end) { (end-start+1 < len)? len=end-start+1:len; start++; } } if(len == INT_MAX) return 0; return len; } bool sum(vector<int>& nums,int s,int i,int j) { for(int k=i;k<=j;++k) s=s-nums[k]; return s<=0; } };
时间: 2024-10-08 17:06:23