【leetcode】209. Minimum Size Subarray Sum

LeetCode 209---Minimum Size Subarray Sum 题目链接:LeetCode 209Minimum Size Subarray Sum题目大意:求出正整数无序序列中长度最小的子序列的元素和等于目标数,如果没有这样的数,则返回0。实现代码:public class Problem209 { public static void main(String[] args) { // TODO Auto-generated metho 阅读详情

209. Minimum Size Subarray Sum

Given an array of n positive integers and a positive integer s, find the minimal length of a contiguous subarray of which the sum ≥ s. If there isn't one, return 0 instead.

Example: 

Input: s = 7, nums = [2,3,1,2,4,3]
Output: 2
Explanation: the subarray [4,3] has the minimal length under the problem constraint.

Follow up:

If you have figured out the O(n) solution, try coding another solution of which the time complexity is O(n log n). 

题目链接:https://leetcode.com/problems/minimum-size-subarray-sum/

 

解法:双指针-滑动窗口

class Solution {
public:
    int minSubArrayLen(int s, vector<int>& nums) {
        int len = nums.size();
        if(len<=0) return 0;
        int i=0, j=0, minl=INT_MAX, sum=nums[0];
        while(true){
            if(sum<s){
                if(j>=len-1) break;
                else{
                    sum+=nums[++j];
                }
            }else{
                if(j-i+1<minl){
                    minl = j-i+1;
                }
                sum -= nums[i++];
            }
        }
        return minl==INT_MAX?0:minl;
    }
};

tip:minl设成nums.size()+1即可

LeetCode 209. Minimum Size Subarray Sum LeetCode 209. Minimum Size Subarray Sum (Medium),主要知识点:sliding window, binary search(有坑未填),优先级:5 阅读详情

相关推荐

leetcode 209 : Minimum Size Subarray Sum

leetcode : Minimum Size Subarray Sum

西施豆腐渣 9636

Leetcode 209 Minimum Size Subarray Sum

给出一个正整数数组,以及一个正整数s,找到最短的子数组且该子数组的和不小于s。如果不存在这样的子数组,返回0.

(ง •̀_•́)ง 564

[leetcode] 209 Minimum Size Subarray Sum

(一)O(logN)的解法 首先将原数组处理成前项和的形式,这样就保证了数组的有序(注意第一个是0,自己push进去),然后遍历数组,寻找小于等于sum[i]+s的最小的下标,如果找不到,那么break结束。否则继续下去,最后取最小值。 class Solution { public: int minSubArrayLen(int s, vector& nums) { vec

NK_test的博客 3361

LeetCode209Minimum Size Subarray Sum

终于不用找实习了,恢复一下学习的节奏。原题LeetCode209Given an array of n positive integers and a positive integer s, find the minimal length of a contiguous subarray of which the sum ≥ s. If there isn’t one, return 0 inste

上善若水 635

Leetcode 209Minimum Size Subarray Sum - MEDIUM

Leetcode 209Minimum Size Subarray Sum - MEDIUM题目思路题解反思复杂度分析 题目 Given an array of n positive integers and a positive integer s, find the minimal length of a contiguous subarray of which the sum ≥ s. ...

weixin_43621681的博客 322

力扣(LeetCode209 长度最小的子数组 Minimum Size Subarray Sum

209 长度最小的子数组 Minimum Size Subarray Sum 看完这个题直接想到的是三层循环,先固定长度,再遍历,原谅我的愚蠢,我内心也觉得这个很可怕,然后写出来超时了。 然后看了有n的平方,然后想到可以先固定起始位置,再遍历,边遍历边检验: int minSubArrayLen(int target, int* nums, int numsSize){ int i, sum, j, minSublen = numsSize+1; for(i = 0; i < nu.

s_w_s_的博客 339

LeetCode - 209. Minimum Size Subarray Sum

题目 LeetCode - 209. Minimum Size Subarray Sum 题目链接 https://leetcode.com/problems/minimum-size-subarray-sum/ 参考博客 https://www.cnblogs.com/zjutlitao/p/3485545.html 解题思路 采用暴力解决,时间复杂度是O(n3)O(n^3)O(n3),这个基...

virgilshi的博客 176

解题思路:LeetCode209 题 “Minimum Size Subarray Sum

给定一个包含正整数的数组和一个正整数 `target`,找到该数组中满足其和大于等于 `target` 的最小长度子数组,并返回其长度。如果不存在这样的子数组,则返回 0。

侯仕奇的博客 597

LeetCode209. Minimum Size Subarray Sum

一 题目 Given an array ofnpositive integers and a positive integers, find the minimal length of acontiguoussubarray of which the sum ≥s. If there isn't one, return 0 instead. Example: Input: s...

bohu83的博客 301

NYUer | LeetCode209 Minimum Size Subarray Sum

NYUer | LeetCode209 Minimum Size Subarray Sum

Moses_SU的博客 267

leetcode: 209. Minimum Size Subarray Sum

leetcode: 209. Minimum Size Subarray Sum 原题链接 解法一:暴力,时间复杂度O(n^2) class Solution { public: int minSubArrayLen(int target, vector<int>& nums) { int n = INT32_MAX; // 不可能达到的数 for (int i = 0; i < nums.size(); i++) {

LSK的博客 153

LeetCode209 Minimum Size Subarray Sum

Given an array ofnpositive integers and a positive integers, find the minimal length of acontiguoussubarray of which the sum ≥s. If there isn't one, return 0 instead. Example: Input: s = 7, ...

李歇特冯-兹拜因巴哈的博客 103

Leetcode 209. Minimum Size Subarray Sum

Leetcode 209. Minimum Size Subarray Sum @(Algo Probs)[LeetCode, Algorithm, Two Pointers] Given an array of n positive integers and a positive integer s, find the minimal length of a contiguous su...

TooSchoolForCool 401

leetcode209. Minimum Size Subarray Sum

建议再想一个时间复杂度是nlogn的方法。但是这题还提供了一个深入的跟进思考。暴力解法在leetcode中会超时。也可以用双指针滑动窗口来进行。可以用暴力解法来进行。以及双循环变量使用时。

ahahayaa的博客 534

LeetCode 209Minimum Size Subarray Sum

题目描述 Given an array of n positive integers and a positive integer s, find the minimal length of a contiguous subarray of which the sum ≥ s. If there isn’t one, return 0 instead. Example: Input: s = 7, nums = [2,3,1,2,4,3] Output: 2 Explanation: the subarra

iCode_girl的博客 137
上一篇: 【leetcode】215. Kth Largest Element in an Array
下一篇: 【leetcode】3. Longest Substring Without Repeating Characters
lemonade13
博客等级 码龄11年 11粉丝 253原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值