209. Minimum Size Subarray Sum
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.
代码如下:(超时)
public class Solution {
public int minSubArrayLen(int s, int[] nums) {
// int min=Integer.MAX_VALUE;
int min=nums.length+1;
if(nums.length==0)
return 0;
for(int i=0;i<nums.length-1;i++)
{
int sum=nums[i],count=1;
if(sum>=s)
return count;
for(int j=i+1;j<nums.length;j++)
{
sum+=nums[j];
count++;
if(count>=min)
break;
if(sum>=s)
{
if(count<min)
min=count;
break;
}
}
}
if(min==nums.length+1)
return 0;
return min;
}
}
209. Minimum Size Subarray Sum的更多相关文章
- 【刷题-LeetCode】209. Minimum Size Subarray Sum
Minimum Size Subarray Sum Given an array of n positive integers and a positive integer s, find the m ...
- [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 ...
- 209. Minimum Size Subarray Sum(双指针)
Given an array of n positive integers and a positive integer s, find the minimal length of a contigu ...
- LeetCode OJ 209. Minimum Size Subarray Sum
Given an array of n positive integers and a positive integer s, find the minimal length of a subarra ...
- LeetCode 209 Minimum Size Subarray Sum
Problem: Given an array of n positive integers and a positive integer s, find the minimal length of ...
- 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 ...
- 【LeetCode】209. Minimum Size Subarray Sum 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/minimum- ...
- Java for LeetCode 209 Minimum Size Subarray Sum
Given an array of n positive integers and a positive integer s, find the minimal length of a subarra ...
- 【Leetcode】209. Minimum Size Subarray Sum
Question: Given an array of n positive integers and a positive integer s, find the minimal length of ...
随机推荐
- Several ports (8005, 8080, 8009) required by Tomcat v7.0 Server at localhost are already in use.
Several ports (8005, 8080, 8009) required by Tomcat v7.0 Server at localhost are already in use. The ...
- model.addAttribute("student",student)——渲染
往前台视图传参数,(将在处理器传入的模型数据在视图jsp中展示出来,即为渲染) 按照spring一般的编码习惯,model 应该是contrller里面的Map结构吧.Map里面添加key=" ...
- discuz 系列产品 在ie9下注册成功后不跳转bug处理
header.htm 把 <meta http-equiv="x-ua-compatible" content="ie=7" /> 改为 <m ...
- Scss sass
http://www.ruanyifeng.com/blog/2012/06/sass.htmlscss 声明:1,$blue : #1875e7;2,.class1 { border: 1px so ...
- $lookup
db.orders.aggregate([ { $lookup: { from: "inventory", localField: "item", foreig ...
- 【C语言学习】-03 循环结构
本文目录 循环结构的特点 while循环 do...while循环 for循环 回到顶部 一.循环结构的特点 程序的三种结构: 顺序结构:顺序执行语句 分支结构:通过进行一个判断在两个可选的语句序列之 ...
- JAVA小记
关于重写equals()方法和重写toString()方法,一般来说,Objects的默认子类都重写了这两个方法,直接利用就行了: 对于用户自定义的类,如果要用到这两方法,就必须在程序中重写.
- [转] lib和dll 区别,生成及使用方法
lib 和 dll 的区别.生成以及使用详解 [目录] lib dll介绍 生成动态库 调用动态库 生成静态库 调用静态库 首先介绍一下静态库(静态链接库).动态库(动态链接库)的概念,首先两者都是代 ...
- SrcollView分页加载数据(第二种方法 自定义listView)
package com.baidu.ms; import android.content.Context;import android.util.AttributeSet;import android ...
- linux exec用法总结
Linux中exec的用法总结 先总结一个表: exec命令 作用 exec ls 在shell中执行ls,ls结果显示结束后不返回原来的的目录中,而是/(根目录) exec <file 将fi ...