别人的代码

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. 提升web响应速度的思路

    web响应(主要指加载网页类,不包括大文件下载,看视频)的核心瓶颈在于延迟,不在于带宽. 从感性认知的角度,由于存在tcp的慢启动,所以往往速率还未达到带宽值时,访问就已经结束:另外,没有交互就没有延 ...

  2. ZooKeeper启动过程2:FastLeaderElection

    前一篇文章中说到,启动ZooKeeper集群时,需要分别启动集群中的各个节点,各节点以QuorumPeer的形式启动,最后到达startLeaderElection和lookForLeader. 先说 ...

  3. Visual Studio 2013 如何关闭调试而不关闭IIS Express

    在VS主面板打开:工具->选项->调试->编辑继续   取消选中[启用"编辑并继续"] 就OK了 (英文版的请对应相应的操作) 不过这是针对所有的调试,如果你想针 ...

  4. 针对初学者的A*算法入门详解(附带Java源码)

    英文题目,汉语内容,有点挂羊头卖狗肉的嫌疑,不过请不要打击我这颗想学好英语的心.当了班主任我才发现大一18本书,11本是英语的,能多用两句英语就多用,个人认为这样也是积累的一种方法. Thanks o ...

  5. spark与Hadoop区别

    2分钟读懂Hadoop和Spark的异同 2016.01.25 11:15:59 来源:51cto作者:51cto ( 0 条评论 )   谈到大数据,相信大家对Hadoop和Apache Spark ...

  6. ios开发之触碰动画效果

    步骤:1.使用singe view application创建新的项目 2.在viewcontroller.h文件中定义两张图片的实例对象,创建一个UIDynamicAnimator实例对象,添加一个 ...

  7. Python 字符串处理大全.

    Python 字符串 字符串是Pyhton中常用的数据类型,我们可以使用引号来创建字符串 . 创建字符串很简单 , 就不说了 . Python 访问字符串中的值 鬼叔本着简洁 使用的设计目的 , 在设 ...

  8. iOS:音频

    ios中有很多支持音频的控件,如:播放本地音乐(file URL)的AVAudioPlayer和AudioToolbox.Framework.可以播放音乐库音乐的MPMusicPlayerContro ...

  9. [图论]Dijkstra 算法小结

    Dijkstra 算法小结  By Wine93 2013.11 1. Dijkstra 算法相关介绍 算法阐述:Dijkstra是解决单源最短路径的算法,它可以在O(n^2)内计算出源点(s)到图中 ...

  10. WP8.1 实现Continuation程序(打开文件,保存文件等)

    以保存文件为例 首先,在项目中加入ContinuationManager.cs类,以及SuspensionManager.cs类. 其次,在App.xaml.cs中,完成如下步骤: 1. 添加Cont ...