别人的代码

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;
}
};

nlogn的解法:把数组依次相加,然后用二分查找法找到sum-nums[i].不高效,但是一个思路。

int minSubArrayLen(int s, vector<int>& nums) {
vector<int> sums = accumulate(nums);
int n = nums.size(), minlen = INT_MAX;
for (int i = 1; i <= n; i++) {
if (sums[i] >= s) {
int p = upper_bound(sums, 0, i, sums[i] - s);
if (p != -1) minlen = min(minlen, i - p + 1);
}
}
return minlen == INT_MAX ? 0 : minlen;
}
private:
vector<int> accumulate(vector<int>& nums) {
int n = nums.size();
vector<int> sums(n + 1, 0);
for (int i = 1; i <= n; i++)
sums[i] = nums[i - 1] + sums[i - 1];
return sums;
}
int upper_bound(vector<int>& sums, int left, int right, int target) {
int l = left, r = right;
while (l < r) {
int m = l + ((r - l) >> 1);
if (sums[m] <= target) l = m + 1;
else r = m;
}
return sums[r] > target ? r : -1;
}

  

LeetCode() Minimun Size Subarray Sum的更多相关文章

  1. [LeetCode] Minimum Size Subarray Sum 解题思路

    Given an array of n positive integers and a positive integer s, find the minimal length of a subarra ...

  2. [LeetCode] Maximum Size Subarray Sum Equals k 最大子数组之和为k

    Given an array nums and a target value k, find the maximum length of a subarray that sums to k. If t ...

  3. [LeetCode] Minimum Size Subarray Sum 最短子数组之和

    Given an array of n positive integers and a positive integer s, find the minimal length of a subarra ...

  4. (leetcode)Minimum Size Subarray Sum

    Given an array of n positive integers and a positive integer s, find the minimal length of a subarra ...

  5. LeetCode Maximum Size Subarray Sum Equals k

    原题链接在这里:https://leetcode.com/problems/maximum-size-subarray-sum-equals-k/ 题目: Given an array nums an ...

  6. LeetCode Minimum Size Subarray Sum (最短子序列和)

    题意:给一个序列,找出其中一个连续子序列,其和大于s但是所含元素最少.返回其长度.0代表整个序列之和均小于s. 思路:O(n)的方法容易想.就是扫一遍,当子序列和大于s时就一直删减子序列前面的一个元素 ...

  7. LeetCode—Minimum Size Subarray Sum

    题目: Given an array of n positive integers and a positive integer s, find the minimal length of a sub ...

  8. leetcode面试准备:Minimum Size Subarray Sum

    leetcode面试准备:Minimum Size Subarray Sum 1 题目 Given an array of n positive integers and a positive int ...

  9. [LeetCode] 325. Maximum Size Subarray Sum Equals k 和等于k的最长子数组

    Given an array nums and a target value k, find the maximum length of a subarray that sums to k. If t ...

随机推荐

  1. Program L 暴力求解

    Description The GeoSurvComp geologic survey company is responsible for detecting underground oil dep ...

  2. HTML参考

    HTML Basic Document <html> <head> <title>Document name goes here</title> < ...

  3. OpenCV函数解读之groupRectangles

    不管新版本的CascadeClassifier,还是老版本的HAAR检测函数cvHaarDetectObjects,都使用了groupRectangles函数进行窗口的组合,其函数原型有以下几个: C ...

  4. Html获取经纬度

    if (navigator.geolocation) { navigator.geolocation.getCurrentPosition( function success(pos) {alert( ...

  5. DirectX 总结和DirectX 9.0 学习笔记

    转自:http://www.cnblogs.com/graphics/archive/2009/11/25/1583682.html DirectX 总结 DDS DirectXDraw Surfac ...

  6. MATLAB里的正则表达式 [转]

    正则表达式在处理字符串及文本时显得十分方便,在perl, python等脚本语言,以及java, .net等平台上都支援正则表达式.事实上,在MATLAB中也提供了正则表达式的支持.主要包含三个常用的 ...

  7. javascript splice

    //arrayObject.splice(index,howmany,element1,.....,elementX)//index 必需.规定从何处添加/删除元素.(0)//howmany 必需.规 ...

  8. LINQ学习之旅(六)

    Insert/Update/Delete操作 插入(Insert) 1.简单形式 说明:new一个对象,使用InsertOnSubmit方法将其加入到对应的集合中,使用SubmitChanges()提 ...

  9. Sticks_dfs

    Description George took sticks of the same length and cut them randomly until all parts became at mo ...

  10. 7、C#基础整理(类)

    String类 概念:是一个class类型的类,里面包含许多处理字符串的方法和属性 1.length方法. 例: string s = "hello"; Console.Write ...