算法专题二:滑动窗口

1.长度最小的子数组

题目链接:209. 长度最小的子数组 - 力扣(LeetCode)

class Solution {
    public int minSubArrayLen(int target, int[] nums) {
        int sum =0;
        int len=Integer.MAX_VALUE;
        int n =nums.length;
        for(int left=0,right =0;right<n;right++){
            sum+=nums[right];
            while(sum>=target){
                len=Math.min(len,right-left+1);
                sum-=nums[left++];
            }
        }
        return len==Integer.MAX_VALUE?0:len;
        
    }
}

2.⽆重复字符的最⻓⼦串

题目链接:3. 无重复字符的最长子串 - 力扣(LeetCode)

class Solution {
    public int lengthOfLongestSubstring(String ss) {
        int[] hash=new int[128];
        char[] s=ss.toCharArray();
        int n=ss.length();
        int left=0,right=0;
        int ret=0;
        while(right<n){
            hash[s[right]]++;
            while(hash[s[right]]>1){
                hash[s[left++]]--;
            }
            ret=Math.max(ret,right-left+1);
            right++;
        }
        return ret;        
    }
}

 注:

hash[s[left++]]--  的理解:

s[left] 表示当前窗口最左边的那个字符。

hash[s[left]]-- 表示:把这个字符在窗口中的出现次数 -1,相当于这个字符被“移出了窗口”。

left++ 表示:把窗口左边界向右移动一格。

hash[s[left++]]--  的执行顺序:

先执行 hash[s[left]]--(外面的 --),再执行 left++(里面的 ++)

3.最大连续1的个数

题目链接:1004. 最大连续1的个数 III - 力扣(LeetCode)

class Solution {
    public int longestOnes(int[] nums, int k) {
        int n=nums.length;
        int ret=0;
        for(int right=0,left=0,zero=0;right<n;right++){
            if(nums[right]==0){
                zero++;
            }
            while(zero>k){
                if(nums[left]==0){
                    zero--;
                }
                left++;
            }
            ret=Math.max(ret,right-left+1);
        }
        return ret;
    }
}

4.将x减小到0的最小操作数

题目链接:1658. 将 x 减到 0 的最小操作数 - 力扣(LeetCode)

class Solution {
    public int minOperations(int[] nums, int x) {
        int sumAll=0;
        int sum=0;
        for(int i=0;i<nums.length;i++){
            sumAll+=nums[i];
        }
        int target=sumAll-x;
        if(target<0){
            return -1;
        }
        int ret=-1;
        for(int right=0,left=0;right<nums.length;right++){
            sum+=nums[right];
            while(sum>target){
                sum-=nums[left++];
            
            }
            if(sum==target){
                ret=Math.max(ret,right-left+1);
            }
        }
        if(ret==-1){
            return ret;
        }else{
            return nums.length-ret;
        }
    }
}

5.水果成篮

题目链接:904. 水果成篮 - 力扣(LeetCode)

方法1:

通过定义一个容器的方法

class Solution {
    public int totalFruit(int[] f) {
        Map<Integer,Integer> hash=new HashMap<Integer,Integer>();
        int ret=0;
        for(int right=0,left=0;right<f.length;right++){
            int in =f[right];
            hash.put(in,hash.getOrDefault(in,0)+1);
            while(hash.size()>2){
                int out=f[left];
                hash.put(out,hash.get(out)-1);
                if(hash.get(out)==0){
                    hash.remove(out);
                }
                left++;
            }
            ret=Math.max(ret,right-left+1);
        }
        return ret;
    }
}

我们在提交后,发现我们的执行用时很长

所以我们想到了另外的一种方法,定义一个哈希数组的方式

方法2:

class Solution {
    public int totalFruit(int[] f) {
        int n =f.length;
        int[] hash=new int[n+1];
        int ret=0;
        for(int left=0,right=0,kinds=0;right<n;right++){
            int in =f[right];
            if(hash[in]==0){
                kinds++;
            }
            hash[in]++;
            while(kinds>2){
                int out=f[left];
                hash[out]--;
                if(hash[out]==0){
                kinds--;
                }
                left++;
            }
            ret=Math.max(ret,right-left+1);
        }
        return ret;
    }
}

我们可以发现执行用时减少了很多。

6.找到字符串中的所有字母异位词

题目链接:438. 找到字符串中所有字母异位词 - 力扣(LeetCode)

class Solution {
    public List<Integer> findAnagrams(String ss, String pp) {
        List<Integer> ret=new ArrayList<>();
        char[] s=ss.toCharArray();
        char[] p=pp.toCharArray();
        int[] hash=new int[26];
        int[] hash2=new int[26];
        int m=p.length;
        for(char ch:p){
            hash[ch-'a']++;
        }
        for(int right=0,left=0,count=0;right<s.length;right++){
            char in =s[right];
            hash2[in-'a']++;
            if(hash2[in-'a']<=hash[in-'a']){
                count++;
            }
            if(right-left+1>m){
                char out=s[left];
                if(hash2[out-'a']<=hash[out-'a']){
                    count--;
                }
                left++;
                hash2[out-'a']--;
            }
            if(count==m){
                ret.add(left);
            }
        }
        return ret;
    }
}

7.串联所有单词的子串

题目链接:30. 串联所有单词的子串 - 力扣(LeetCode)

class Solution {
    public List<Integer> findSubstring(String s, String[] words) {
        List<Integer> ret=new ArrayList<>();
        Map<String,Integer> hash=new HashMap<String,Integer>();
        int len=words[0].length();
        int m=words.length;
        for(String sh:words){
            hash.put(sh,hash.getOrDefault(sh,0)+1);
        }
        for(int i=0;i<len;i++){
            Map<String,Integer> hash2=new HashMap<String,Integer>();
            for(int right=i,left=i,count=0;right+len<=s.length();right+=len){
                String in =s.substring(right,right+len);
                hash2.put(in,hash2.getOrDefault(in,0)+1);
                if(hash2.get(in)<=hash.getOrDefault(in,0)){
                    count++;
                }
                if(right-left+1>m*len){
                    String out=s.substring(left,left+len);
                    if(hash2.get(out)<=hash.getOrDefault(out,0)){
                    count--;
                }
                hash2.put(out,hash2.getOrDefault(out,0)-1);
                left+=len;
                }
                if(count==m){
                    ret.add(left);
                }
            }
            
        }
        return ret;
    }
}

8.最小覆盖子串

题目链接:76. 最小覆盖子串 - 力扣(LeetCode)

class Solution {
    public String minWindow(String ss, String tt) {
        int[] hash=new int[128];
        int[] hash2=new int[128];
        char[] s=ss.toCharArray();
        char[] t=tt.toCharArray();
        int kinds=0;
        for(char ch:t){
            if(hash[ch]==0){
                kinds++;
            }
            hash[ch]++;
        }
        int minlen=Integer.MAX_VALUE,begin=-1;

        for(int right=0,left=0,count=0;right<s.length;right++){
            char in =s[right];
            hash2[in]++;
            if(hash2[in]==hash[in]){
                count++;
            }
            while(count==kinds){
                if(right-left+1<minlen){
                    minlen=right-left+1;
                    begin=left;
                }
                char out=s[left];
                left++;
                if(hash[out]==hash2[out]){
                    count--;
                }
                hash2[out]--;

            } 
            
        }
        if(begin==-1){
                return new String();
            }else{
                return ss.substring(begin,begin+minlen);
            }
        }
}

希望能对大家有所帮助!!!!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值