LeetCode() Minimun Size Subarray Sum
别人的代码
- 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的更多相关文章
- [LeetCode] Minimum Size Subarray Sum 解题思路
Given an array of n positive integers and a positive integer s, find the minimal length of a subarra ...
- [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 ...
- [LeetCode] Minimum Size Subarray Sum 最短子数组之和
Given an array of n positive integers and a positive integer s, find the minimal length of a subarra ...
- (leetcode)Minimum Size Subarray Sum
Given an array of n positive integers and a positive integer s, find the minimal length of a subarra ...
- LeetCode Maximum Size Subarray Sum Equals k
原题链接在这里:https://leetcode.com/problems/maximum-size-subarray-sum-equals-k/ 题目: Given an array nums an ...
- LeetCode Minimum Size Subarray Sum (最短子序列和)
题意:给一个序列,找出其中一个连续子序列,其和大于s但是所含元素最少.返回其长度.0代表整个序列之和均小于s. 思路:O(n)的方法容易想.就是扫一遍,当子序列和大于s时就一直删减子序列前面的一个元素 ...
- LeetCode—Minimum Size Subarray Sum
题目: Given an array of n positive integers and a positive integer s, find the minimal length of a sub ...
- leetcode面试准备:Minimum Size Subarray Sum
leetcode面试准备:Minimum Size Subarray Sum 1 题目 Given an array of n positive integers and a positive int ...
- [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 ...
随机推荐
- MonoRail学习-入门实例篇
1.到官方网站下载安装文件,地址如下: http://www.castleproject.org/index.php/Castle:Download目前最新版本Beta5(您也可以不需要下载,直接使用 ...
- APP store 审核注意点
磨刀不误砍柴工.作为手机应用开发者,你需要向应用商店提交应用审核,迅速通过审核可以让你抢占先机.对苹果iOS应用开发者来说尤其如此.苹果应用商店的审核近乎吹毛求疵,下面这些清单可以让你知道苹果会在哪些 ...
- C# IList<T>转为DataTable
public class WebUtil { /// <summary> /// 转换IList<T>为DataTable/// </summary> /// &l ...
- MVC5 烂笔头
HttpContent Controller:HttpContextBase View:HttpContext.Current View的搜寻顺序:本文件夹.本共享.根共享等 class=" ...
- yum源的更新问题
我们知道在linux下安装软件的方法有多种多样,其中利用yum的方式来安装较为简单,但需要等待的时间比较长.下面介绍一下如何更新yum的源的问题. 首先需要保证的是linux的机器能上网.然后按照下面 ...
- yii2 生成PDF格式的文件
1 .先把mpdf-development.zip解压的类文件夹放到vendor目录里面,重命名为mpdf 2 .在vendor/composer/autoload_namespaces.php里面添 ...
- Minimum Inversion Number_线段树||树状数组
Problem Description The inversion number of a given number sequence a1, a2, ..., an is the number of ...
- Supermarket_贪心
Description A supermarket has a set Prod of products on sale. It earns a profit px for each product ...
- event.keyCode|| event.which.的用法
HTML 用户名:<input type="text" id="UserAccount" onKeyPress="JumpByEnter(Use ...
- 五、CCNode
本将主要介绍下CCNode这个类,CCNode是所有节点的基类,其中包括我们常用的CCScene(场景).CCLayer(图层).CCSprite(精灵)等,它是一个不能够可视化显示的抽象类,只是用来 ...