滑动窗口
给定一个含有 n 个正整数的数组和一个正整数 target 。
找出该数组中满足其总和大于等于 target 的长度最小的 连续子数组 [numsl, numsl+1, ..., numsr-1, numsr] ,并返回其长度**。**如果不存在符合条件的子数组,返回 0 。
解法:滑动窗口,不断移动左右位置控制窗口大小
class Solution {
public int minSubArrayLen(int target, int[] nums) {
int n = nums.length;
int left =0,right =0;
int sums=0;
int result=Integer.MAX_VALUE;
if(n==0){
return 0;
}
while(right< n){
sums +=nums[right];
while(sums >=target){
result = Math.min(result,right-left+1);
sums -=nums[left];
left++;
}
right++;
}
return result==Integer.MAX_VALUE?0:result;
}
}给定一个字符串 s ,请你找出其中不含有重复字符的 最长子串 的长度。
还是滑动窗口的实现,用一个set来存储是否包含之前的字符。
给定一个字符串 s 和一个字符串数组 words。 words 中所有字符串 长度相同。
s 中的 串联子串 是指一个包含 words 中所有字符串以任意顺序排列连接起来的子串。
例如,如果
words = ["ab","cd","ef"], 那么"abcdef","abefcd","cdabef","cdefab","efabcd", 和"efcdab"都是串联子串。"acdbef"不是串联子串,因为他不是任何words排列的连接。
返回所有串联子串在 s 中的开始索引。你可以以 任意顺序 返回答案。
解答:-----这是定长窗口
还是使用滑动数组,因为需要包含words中的所有字符串,所以窗口长度为num * wordLen,因为是单词,所以多了一个单词长度wordLen的for循环。在内层遍历的时候每次都加wordLen。
给你一个字符串 s 、一个字符串 t 。返回 s 中涵盖 t 所有字符的最小子串。如果 s 中不存在涵盖 t 所有字符的子串,则返回空字符串 "" 。
解法:滑动窗口,在遍历中不断调整窗口大小。
最后更新于
这有帮助吗?