别人的代码

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. 0xC0000005: 读取位置 0x00000000 时发生访问冲突

    遇见这种问题一般都是空指针,即:指针里没有赋值~ 如果你对null 进行操作就会产生空指针异常 Object obj = new Object(); 你要知道 obj是一个Object指针变量,指向O ...

  2. android 录音的断点续传

    系统没有暂停的功能  只能把每次的录音进行拼接... package com.example.zrecord; import java.io.File;import java.io.FileInput ...

  3. Unity截屏

    方式一:直接使用unity自带的截图函数 Application.CaptureScreenshot(“imagename”); 保存路径: 在PC上保存路径为Application.dataPath ...

  4. 【python练习】截取网页里最新的新闻

    需求: 在下面这个网页,抓取最新的新闻,按天划分. http://blog.eastmoney.com/13102551638/bloglist_0_1.html 实现方法1:使用递归 import ...

  5. 理解Mach Port

    参考文档: 1. http://robert.sesek.com/thoughts/2012/1/debugging_mach_ports.html 2. Mach 3 Kernel Interfac ...

  6. Java基础毕向东day04

    1. 数组 2.选择排序.冒泡排序.折半查找.

  7. SDIO接口

    SDIO卡是在SD内存卡接口的基础上发展起来的接口,SDIO接口兼容以前的SD内存卡,并且可以连接SDIO接口的设备,目前根据SDIO协议的SPEC,SDIO接口支持的设备总类有蓝牙,网卡,电视卡等. ...

  8. cookie 内容的获取

    NSMutableArray *cookiesStoreAll = [[NSMutableArray alloc]init]; NSUInteger totalNumberOfCookies;     ...

  9. ERP仓库管理系统查询(十)

    需求:    1.根据仓库编号,获取仓库信息绑定至页面相关控件. 2.根据仓库编号,获取管理员信息绑定到页面相关控件 修改的界面: <%@ Page Language="C#" ...

  10. 通过 itms-services 协议,发布或者分享 iOS 应用程序

    导读:itms-services 协议常用于 iOS 企业应用的无线部署,这可在不使用 iTunes 的情况下将内部软件发布或者分享给用户. 一.前期准备资料: 1.应用程序 (.ipa) 文件(使用 ...