【leetcode】Minimum Size Subarray Sum(middle)
Given an array of n positive integers and a positive integer s, find the minimal length of a subarray of which the sum ≥ s. If there isn't one, return 0 instead.
For example, given the array [2,3,1,2,4,3] and s = 7,
the subarray [4,3] has the minimal length under the problem constraint.
思路:用start, end两个游标来记录范围,sum < s end就向后走, s >= sum start就向后走。
我写的代码没有大神的逻辑清晰,先上大神的。
int minSubArrayLen(int s, vector<int>& nums) {
int firstPos = , sum = , minLength = INT_MAX;
for(int i = ; i<nums.size(); i++) { //i即end游标 对所有end游标循环
sum += nums[i];
while(sum >= s) { //对每个end游标的start游标循环 firstPos即为start游标 只有s >= sum 时才把start向后移
minLength = min(minLength, i - firstPos + );
sum -= nums[firstPos++];
}
}
return minLength == INT_MAX? : minLength; //没找到s >= sum 时返回0
}
我的代码乱一点,但是也AC了。
int minSubArrayLen(int s, vector<int>& nums) {
int start = , end = ;
int sum = ;
int minLength = nums.size() + ;
while(end <= nums.size()) //有等于是因为结尾到最后面时 起始点还可能移动
{
if(sum < s)
{
if(end == nums.size()) break;
sum += nums[end++];
}
else
{
minLength = (minLength < (end - start)) ? minLength : (end - start);
sum -= nums[start++];
}
}
minLength = (minLength == nums.size() + ) ? : minLength; //没找到符合条件的子序列 返回0
return minLength;
}
【leetcode】Minimum Size Subarray Sum(middle)的更多相关文章
- LeetCode 209. Minimum Size Subarray Sum (最短子数组之和)
Given an array of n positive integers and a positive integer s, find the minimal length of a contigu ...
- 【数组】Minimum Size Subarray Sum
题目: Given an array of n positive integers and a positive integer s, find the minimal length of a sub ...
- 【leetcode】Search for a Range(middle)
Given a sorted array of integers, find the starting and ending position of a given target value. You ...
- 【leetcode】Evaluate Reverse Polish Notation(middle)
Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...
- 【leetcode】Container With Most Water(middle)
Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). ...
- 【leetcode】Swap Nodes in Pairs (middle)
Given a linked list, swap every two adjacent nodes and return its head. For example,Given 1->2-&g ...
- 【leetcode】Linked List Cycle II (middle)
Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Foll ...
- 【leetcode】Reverse Linked List II (middle)
Reverse a linked list from position m to n. Do it in-place and in one-pass. For example:Given 1-> ...
- 【leetcode】Binary Search Tree Iterator(middle)
Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the ro ...
随机推荐
- eclipse svn快捷键
一.打开eclipse插件安装市场,搜索svn,选择Subclipse安装 二.设置 svn ,设置快捷键, 1.windows-preference,在打开对话框输入keys过滤出keys选择 2. ...
- JSON 问题
{"statusCode":"300","message":"栏目插入出现故障==bannerInfoService.add 栏目 ...
- spring框架搭建url
MyEclipse+Tomcat+MAVEN+SVN项目完整环境搭建 http://blog.csdn.net/zhshulin/article/details/30779873 MyEclipse下 ...
- 如何编写可维护的面向对象JavaScript代码
能够写出可维护的面向对象JavaScript代 码不仅可以节约金钱,还能让你很受欢迎.不信?有可能你自己或者其他什么人有一天会回来重用你的代码.如果能尽量让这个经历不那么痛苦,就可以节省不少时 间.地 ...
- Lab1--关于安装JUnit的简要描述
安装JUnit的过程描述: 下载两个jar包: hamcrest-all-1.3.jar junit-4.12.jar 注意在导入完成jar包之后不要随意改变jar包的路径. 创建java程序,书写如 ...
- Windbg学习使用
WinDbg是微软发布的一款相当优秀的源码级(source-level)调试工具,可以用于Kernel模式调试和用户模式调试,还可以调试Dump文件. 1. WinDbg介绍: Debuggin ...
- php面向对象面试题
php面试题之四--PHP面向对象(基础部分) 四.PHP面向对象 1. 写出 php 的 public.protected.private 三种访问控制模式的区别(新浪网技术部) public:公有 ...
- Javascript正则表达式匹配替换
根据正则表达式的匹配结果将匹配项替换为*function regReplace(reg, str){ var result, //最终输出结果 out, //每次运行正则exec返回的匹配结果. in ...
- tone mapping简介
以下内容转自网络: tone Mapping原是摄影学中的一个术语,因为打印相片所能表现的亮度范围不足以表现现实世界中的亮度域,而如果简单的将真实世界的整个亮度域线性压缩到照片所能表现的亮度域内,则会 ...
- Java 的printf(转)
出处:http://blog.csdn.net/swandragon/article/details/4653600 public class TestPrintf{public static voi ...