class Solution {
public:int minSubArrayLen(int target, vector<int>& nums) {// 长度最小的子数组 // 大于等于 targetint min_len = INT32_MAX;// 总和int sum = 0;int start = 0; // 起点for(int i = 0; i< nums.size(); i++) {sum += nums[i];while(sum >= target) {int len = i-start+1;if(len < min_len ) {min_len = len;} sum -= nums[start++];} }if(min_len == INT32_MAX ) {return 0; } else {return min_len;}}
};
3. 无重复字符的最长子串
class Solution {
public:int lengthOfLongestSubstring(string s) {// 长度int str_size = s.size();if(str_size == 0) {return 0;}int max_len = 0;int left = 0;unordered_set<char> str; // 用来存储 字符for(int i = 0; i< str_size ; i++ ){// 如果没有找到 该字符while(str.find(s[i]) != str.end()) {str.erase(s[left]);left++;}max_len = max(max_len, i- left + 1);// 插入str.insert(s[i]);}return max_len;}
};