别人的代码

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. Cisco IOS Debug Command Reference I through L

    debug iapp through debug ip ftp debug iapp : to begin debugging of IAPP operations(in privileged EXE ...

  2. vi编辑器选项

    Vi编辑器有一些选项设置可以帮助人们更好的使用. 在vi中选项分为两种: 1.  开关选项,如果要打开这类选项就使用ex命令——:set 选项:如果要关闭这类选项就是用ex命令——:set  no选项 ...

  3. [windows驱动]标准驱动例程

    [注]routine:例行程序. 1.标准驱动例程简介: 每一个内核态驱动程序都是由一系列系统定义的,标准的驱动例程组成.内核态驱动在这些标准例程中通过调用系统提供的驱动支持函数处理I/O请求包.为了 ...

  4. Map-Reduce的工作机制

    Mapper “Map-Reduce”的思想就是“分而治之” Mapper负责“分”,即把复杂的任务分解为若干个“简单的任务”而执行 “简单的任务”有几个意思:1.数据或计算规模相对于原任务要大大缩小 ...

  5. SharePoint 2013 开发——构建工作流开发环境

    博客地址:http://blog.csdn.net/FoxDave 本篇我们来讲述一下如何搭建SharePoint 2013工作流开发环境. Windows Azure Workflow作为单独的可下 ...

  6. 命令行BASH

    shell 壳,把用户的指令翻译给内核kernel,真正工作的是内核 shell分为cli(command line interface)和gui(graphical user interface) ...

  7. C#基础之类、组件和命名空间(二)

    一.实例化对象 Student s; 首先是在栈中开辟一块空间叫s,s里面的内容是空: s = new Student(); 在堆实例化Student对象,将对象的引用地址保存到栈s里.因此,s指向S ...

  8. oracle中的cluster表

    大家对通常oracle中的cluster的理解是不准确的,经常和sql server中的cluster index混淆.Cluster是存储一组table的一种方法,这些table共享同一数据块中的某 ...

  9. du -sh 目录名称 查看目录大小

    du -sh 目录名称 查看目录大小 df -h 查看磁盘使用情况

  10. 练手CF3-C - Wormhouse

    深搜,亮点在那个剪枝,flag代表是否搜索数组从开始到当前一直等于原始数组同位置的数,如果是真,就从原始数组的当前位置的书开始搜,否则就从0开始搜. 见代码. #include <iostrea ...