别人的代码

  1. class Solution {
  2. public:
  3. int minSubArrayLen(int s, vector<int>& nums) {
  4. int l, r, cum, res = nums.size()+1;
  5. l = r = cum = 0;
  6. while ((unsigned int)r < nums.size()) {
  7. cum += nums[r++];
  8. while (cum >= s) {
  9. res = min(res, r-l);
  10. cum -= nums[l++];
  11. }
  12. }
  13. return res<=nums.size()?res:0;
  14. }
  15. };

我的 280ms

  1. class Solution {
  2. public:
  3. int minSubArrayLen(int s, vector<int>& nums) {
  4. vector<int> res;
  5. int start=0,end=0,len=INT_MAX;
  6. for(int end=0;end<nums.size();++end)
  7. {
  8. while(sum(nums,s,start,end) && start<=end)
  9. {
  10. (end-start+1 < len)? len=end-start+1:len;
  11. start++;
  12. }
  13. }
  14. if(len == INT_MAX)
  15. return 0;
  16. return len;
  17. }
  18. bool sum(vector<int>& nums,int s,int i,int j)
  19. {
  20. for(int k=i;k<=j;++k)
  21. s=s-nums[k];
  22. return s<=0;
  23. }
  24. };

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

  1. int minSubArrayLen(int s, vector<int>& nums) {
  2. vector<int> sums = accumulate(nums);
  3. int n = nums.size(), minlen = INT_MAX;
  4. for (int i = 1; i <= n; i++) {
  5. if (sums[i] >= s) {
  6. int p = upper_bound(sums, 0, i, sums[i] - s);
  7. if (p != -1) minlen = min(minlen, i - p + 1);
  8. }
  9. }
  10. return minlen == INT_MAX ? 0 : minlen;
  11. }
  12. private:
  13. vector<int> accumulate(vector<int>& nums) {
  14. int n = nums.size();
  15. vector<int> sums(n + 1, 0);
  16. for (int i = 1; i <= n; i++)
  17. sums[i] = nums[i - 1] + sums[i - 1];
  18. return sums;
  19. }
  20. int upper_bound(vector<int>& sums, int left, int right, int target) {
  21. int l = left, r = right;
  22. while (l < r) {
  23. int m = l + ((r - l) >> 1);
  24. if (sums[m] <= target) l = m + 1;
  25. else r = m;
  26. }
  27. return sums[r] > target ? r : -1;
  28. }

  

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. MonoRail学习-入门实例篇

    1.到官方网站下载安装文件,地址如下: http://www.castleproject.org/index.php/Castle:Download目前最新版本Beta5(您也可以不需要下载,直接使用 ...

  2. APP store 审核注意点

    磨刀不误砍柴工.作为手机应用开发者,你需要向应用商店提交应用审核,迅速通过审核可以让你抢占先机.对苹果iOS应用开发者来说尤其如此.苹果应用商店的审核近乎吹毛求疵,下面这些清单可以让你知道苹果会在哪些 ...

  3. C# IList<T>转为DataTable

    public class WebUtil { /// <summary> /// 转换IList<T>为DataTable/// </summary> /// &l ...

  4. MVC5 烂笔头

    HttpContent Controller:HttpContextBase View:HttpContext.Current View的搜寻顺序:本文件夹.本共享.根共享等 class=" ...

  5. yum源的更新问题

    我们知道在linux下安装软件的方法有多种多样,其中利用yum的方式来安装较为简单,但需要等待的时间比较长.下面介绍一下如何更新yum的源的问题. 首先需要保证的是linux的机器能上网.然后按照下面 ...

  6. yii2 生成PDF格式的文件

    1 .先把mpdf-development.zip解压的类文件夹放到vendor目录里面,重命名为mpdf 2 .在vendor/composer/autoload_namespaces.php里面添 ...

  7. Minimum Inversion Number_线段树||树状数组

    Problem Description The inversion number of a given number sequence a1, a2, ..., an is the number of ...

  8. Supermarket_贪心

    Description A supermarket has a set Prod of products on sale. It earns a profit px for each product ...

  9. event.keyCode|| event.which.的用法

    HTML 用户名:<input type="text" id="UserAccount" onKeyPress="JumpByEnter(Use ...

  10. 五、CCNode

    本将主要介绍下CCNode这个类,CCNode是所有节点的基类,其中包括我们常用的CCScene(场景).CCLayer(图层).CCSprite(精灵)等,它是一个不能够可视化显示的抽象类,只是用来 ...