Home
  • 计算机网络
  • 操作系统
  • 数据结构与算法
  • 设计模式
  • JavaSE
  • JVM
  • JUC
  • Netty
  • CPP
  • QT
  • UE
  • Go
  • Gin
  • Gorm
  • HTML
  • CSS
  • JavaScript
  • vue2
  • TypeScript
  • vue3
  • react
  • Spring
  • SpringMVC
  • Mybatis
  • SpringBoot
  • SpringSecurity
  • SpringCloud
  • Mysql
  • Redis
  • 消息中间件
  • RPC
  • 分布式锁
  • 分布式事务
  • 个人博客
  • 弹幕视频平台
  • API网关
  • 售票系统
  • 消息推送平台
  • SaaS短链接系统
  • Linux
  • Docker
  • Git
GitHub (opens new window)
Home
  • 计算机网络
  • 操作系统
  • 数据结构与算法
  • 设计模式
  • JavaSE
  • JVM
  • JUC
  • Netty
  • CPP
  • QT
  • UE
  • Go
  • Gin
  • Gorm
  • HTML
  • CSS
  • JavaScript
  • vue2
  • TypeScript
  • vue3
  • react
  • Spring
  • SpringMVC
  • Mybatis
  • SpringBoot
  • SpringSecurity
  • SpringCloud
  • Mysql
  • Redis
  • 消息中间件
  • RPC
  • 分布式锁
  • 分布式事务
  • 个人博客
  • 弹幕视频平台
  • API网关
  • 售票系统
  • 消息推送平台
  • SaaS短链接系统
  • Linux
  • Docker
  • Git
GitHub (opens new window)
  • 哈希表

  • 双指针

    • 同向双指针
    • 双向双指针
    • 滑动窗口
      • 3. 无重复字符的最长子串
      • 209. 长度最小的子数组
      • 713. 乘积小于 K 的子数组
      • 424. 替换后的最长重复字符
      • 76. 最小覆盖子串
      • 438. 找到字符串中所有字母异位词
      • 567. 字符串的排列
      • 1156. 单字符重复子串的最大长度
    • 分组循环
  • 数组

  • 字符串

  • 链表

  • 树

  • 回溯

  • 动态规划

  • 图

  • 二分查找

  • 贪心

  • 栈&队列

  • 堆

  • 位运算

  • 数据结构设计

  • rui的精选题单

  • 笔试真题

  • LeetCode周赛

  • ACM模式输入输出
  • 数学

  • 数据结构与算法
  • 双指针
Nreal
2023-11-01
目录

滑动窗口

# 3. 无重复字符的最长子串 (opens new window)

class Solution {
    public int lengthOfLongestSubstring(String s) {
        int[] window = new int[256];
        int l=0,r=0;
        int ans = 0;
        while(r<s.length()){
            char c1 = s.charAt(r++);
            window[c1]++;
            while(window[c1]>1){
                char c2 = s.charAt(l++);
                window[c2]--;
            }
            ans = Math.max(ans,r-l);
        }
        return ans;
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

# 209. 长度最小的子数组 (opens new window)

class Solution {
    public int minSubArrayLen(int target, int[] nums) {
        int n = nums.length;
        int sum=0,l=0;
        int ans = Integer.MAX_VALUE;
        for(int r=0;r<n;r++){
            sum += nums[r];
            while(sum>=target){
                ans = Math.min(ans,r-l+1);
                sum -= nums[l++];
            }
        }
        return ans == Integer.MAX_VALUE?0:ans;
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

# 713. 乘积小于 K 的子数组 (opens new window)

class Solution {
    public int numSubarrayProductLessThanK(int[] nums, int k) {
        if(k<=1) return 0;//k为0,左指针越界
        int n = nums.length;
        int ans=0,mul=1,l=0;
        for(int r=0;r<n;r++){
            mul *= nums[r];
            while(mul>=k){
                mul /= nums[l++];
            }
            ans += r-l+1;
        }
        return ans;
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

# 424. 替换后的最长重复字符 (opens new window)

class Solution {
    public int characterReplacement(String s, int k) {
        int n = s.length();
        int l=0,r=0,ans=0;
        int max=0;
        int[] cnt = new int[26];
        while(r<n){
            int i=s.charAt(r)-'A';
            cnt[i]++;
            max = Math.max(max,cnt[i]);
            while(r-l+1 > max+k){
                cnt[s.charAt(l)-'A']--;
                l++;
            }
            ans = Math.max(ans,r-l+1);
            r++;
        }
        return ans;
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

# 76. 最小覆盖子串 (opens new window)

class Solution {
    public String minWindow(String s, String t) {
        Map<Character,Integer> need = new HashMap<>();
        Map<Character,Integer> window = new HashMap<>();
        for(int i=0;i<t.length();i++){
            char c = t.charAt(i);
            need.put(c,need.getOrDefault(c,0)+1);
        }
        int l=0,r=0;
        int len = Integer.MAX_VALUE;
        int cnt=0;// 记录字母种类
        int resIndex=0;
        while(r<s.length()){
            char c1 = s.charAt(r++);
            if(need.containsKey(c1)){
                window.put(c1,window.getOrDefault(c1,0)+1);
                if(window.get(c1).equals(need.get(c1))){
                    cnt++;
                }
            }
            // 达到缩小窗口条件
            while(cnt == need.size()){
                if(r-l<len){
                    resIndex = l;
                    len = r-l;
                }
                char c2 = s.charAt(l++);
                if(need.containsKey(c2)){
                    if(window.get(c2).equals(need.get(c2))){
                        cnt--;
                    }
                    window.put(c2,window.get(c2)-1);
                }
            }
        }
        return len == Integer.MAX_VALUE?"":s.substring(resIndex,resIndex+len);
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38

# 438. 找到字符串中所有字母异位词 (opens new window)

class Solution {
    public List<Integer> findAnagrams(String s, String p) {
        Map<Character,Integer> window = new HashMap<>();
        Map<Character,Integer> need = new HashMap<>();
        for(int i=0;i<p.length();i++){
            char c = p.charAt(i);
            need.put(c,need.getOrDefault(c,0)+1);
        }
        int l=0,r=0;
        int cnt=0;
        List<Integer> res = new ArrayList<>();
        while(r<s.length()){
            char c1 = s.charAt(r++);
            if(need.containsKey(c1)){
                window.put(c1,window.getOrDefault(c1,0)+1);
                if(window.get(c1).equals(need.get(c1))){
                    cnt++;
                }
            }
            while(r-l==p.length()){
                if(cnt == need.size()){
                    res.add(l);
                }
                char c2 = s.charAt(l++);
                if(need.containsKey(c2)){
                    if(window.get(c2).equals(need.get(c2))){
                        cnt--;
                    }
                    window.put(c2,window.get(c2)-1);
                }
            }
        }
        return res;
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35

# 567. 字符串的排列 (opens new window)

class Solution {
    public boolean checkInclusion(String s1, String s2) {
        Map<Character,Integer> window = new HashMap<>();
        Map<Character,Integer> need = new HashMap<>();
        for(int i=0;i<s1.length();i++){
            char c1 = s1.charAt(i);
            need.put(c1,need.getOrDefault(c1,0)+1);
        }
        int l=0,r=0;
        int cnt=0;
        while(r<s2.length()){
            char c1 = s2.charAt(r++);
            if(need.containsKey(c1)){
                window.put(c1,window.getOrDefault(c1,0)+1);
                if(window.get(c1).equals(need.get(c1))){
                    cnt++;
                }
            }
            while(r-l==s1.length()){
                if(cnt==need.size()){
                    return true;
                }
                char c2 = s2.charAt(l++);
                if(need.containsKey(c2)){
                    if(window.get(c2).equals(need.get(c2))){
                        cnt--;
                    }
                    window.put(c2,window.get(c2)-1);
                }
            }
        }
        return false;
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34

# 1156. 单字符重复子串的最大长度 (opens new window)

双向双指针
分组循环

← 双向双指针 分组循环→

Theme by Vdoing | Copyright © 2021-2024
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式