leetcode 209 长度最小的子数组

简介: leetcode 209 长度最小的子数组

长度最小的子数组

第一次自己答

超出时间范围,时间复杂度太高

#include <iostream>
#include <vector>
using namespace std;
class Solution {
public:
    int minSubArrayLen(int target, vector<int>& nums) {
        int sum = 0, size=0,subsize=0 ;
        for (int j = 0; j < nums.size(); j++)
        {
            for (int i = j; i < nums.size(); i++)
            {
                sum += nums[i];
                size++;
                if (sum >= target)
                {
                    if (subsize == 0) subsize = size;
                    else if (size < subsize)
                    {
                        subsize = size;
                    }
                    size = 0;
                    sum = 0;
                }
            }
            size = 0;
            sum = 0;
        }
        return subsize;
    } 
};
int main()
{
    vector<int> my_nums = { 1,2,3,4,5};
    int target = 15;
    Solution a;
    cout << a.minSubArrayLen(target , my_nums);
  return 0;
}


暴力解法

和自己写的原理类似,都是超时

#include <iostream>
#include <vector>
using namespace std;
class Solution {
public:
    int minSubArrayLen(int s, vector<int>& nums) {
        int result = INT32_MAX; // 最终的结果
        int sum = 0; // 子序列的数值之和
        int subLength = 0; // 子序列的长度
        for (int i = 0; i < nums.size(); i++) { // 设置子序列起点为i
            sum = 0;
            for (int j = i; j < nums.size(); j++) { // 设置子序列终止位置为j
                sum += nums[j];
                if (sum >= s) { // 一旦发现子序列和超过了s,更新result
                    subLength = j - i + 1; // 取子序列的长度
                    result = result < subLength ? result : subLength;
                    break; // 因为我们是找符合条件最短的子序列,所以一旦符合条件就break
                }
            }
        }
        // 如果result没有被赋值的话,就返回0,说明没有符合条件的子序列
        return result == INT32_MAX ? 0 : result;
    }
};
int main()
{
    vector<int> my_nums = { 2,3,1,2,4,3 };
    int target = 7;
    Solution a;
    cout << a.minSubArrayLen(target , my_nums);
  return 0;
}


滑动窗口 (双指针)

#include <iostream>
#include <vector>
using namespace std;
class Solution {
public:
    int minSubArrayLen(int target, vector<int>& nums) {
        int i = 0, sum = 0, length = 0, result = INT32_MAX;
        for (int j = 0; j < nums.size(); j++)
        {
            sum += nums[j];
            while (sum >= target)
            {
                length = j - i + 1;
                result = result < length ? result : length;
                sum -= nums[i++];
            }
        }
        return result ==INT32_MAX ? 0:result;
    }
};
int main()
{
    vector<int> my_nums = { 2,3,1,2,4,3 };
    int target = 7;
    Solution a;
    cout << a.minSubArrayLen(target , my_nums);
  return 0;
}


二刷

class Solution {
public:
    int minSubArrayLen(int target, vector<int>& nums) {
        int result=INT_MAX ,sum=nums[0];
        int left=0,right=0;
        for(right=0 ; right<nums.size() && left<=right;)
        {
            if( sum >= target )
            {
                if( right-left+1 <= result) result = right-left+1 ;
                cout<<left<<' '<<right<<' '<<sum<<endl;
                sum -=  nums[left];
                left++;
            }else
            {
                right++;
                if(right>=nums.size()) break;
                sum += nums[right];
            }
        }
        if(result == INT_MAX) return 0;
        return result;
    }
};


相关文章
|
6天前
|
存储 算法
LeetCode刷题---209. 长度最小的子数组(双指针-滑动窗口)
LeetCode刷题---209. 长度最小的子数组(双指针-滑动窗口)
|
6天前
|
索引
leetcode代码记录(最长重复子数组
leetcode代码记录(最长重复子数组
13 0
|
6天前
leetcode代码记录(长度最小的子数组
leetcode代码记录(长度最小的子数组
11 0
|
6天前
【力扣】209. 长度最小的子数组
【力扣】209. 长度最小的子数组
|
6天前
|
算法 测试技术
每日一题:LeetCode-209. 长度最小的子数组(滑动窗口)
每日一题:LeetCode-209. 长度最小的子数组(滑动窗口)
|
6天前
|
算法
LeetCode题:581. 最短无序连续子数组,242. 有效的字母异位词,202. 快乐数
LeetCode题:581. 最短无序连续子数组,242. 有效的字母异位词,202. 快乐数
30 0
|
6天前
代码随想录 Day44 动规12 LeetCode T300 最长递增子序列 T674 最长连续递增序列 T718 最长重复子数组
代码随想录 Day44 动规12 LeetCode T300 最长递增子序列 T674 最长连续递增序列 T718 最长重复子数组
44 0
|
6天前
|
存储
【Leetcode 209】长度最小的子数组 —— 滑动窗口|双指针
我们可以使用双指针解决本题,定义两个指针 i 和 j 分别表示子数组(滑动窗口窗口)的开始位置和结束位置,维护变量 sum 存储子数组中的元素和。每一轮迭代中,每当 sum >= target 则记录子数组最小长度,移动慢指针。在每一轮迭代最后,移动快指针
|
6天前
|
算法 C++
【刷题】Leetcode 1609.奇偶树
这道题是我目前做过最难的题,虽然没有一遍做出来,但是参考大佬的代码,慢慢啃的感觉的真的很好。刷题继续!!!!!!
9 0
|
6天前
|
算法 索引
【刷题】滑动窗口精通 — Leetcode 30. 串联所有单词的子串 | Leetcode 76. 最小覆盖子串
经过这两道题目的书写,相信大家一定深刻认识到了滑动窗口的使用方法!!! 下面请大家继续刷题吧!!!
13 0