Leetcode Sliding Window
3. Longest Substring Without Repeating Characters
Description
Given a string s, find the length of the longest substring without repeating characters.
Click to view full description
Example 1:
- Input:
s = "abcabcbb" - Output:
3
Example 2:
- Input:
s = "bbbbb" - Output:
1
Solution
Idea: 使用滑动窗口。当窗口内的字符串不包含重复字符时,记录长度,并尝试缩小窗口,即 i 向右移动。缩小到不满足条件时,再尝试扩大窗口,即 j 向右移动。
Complexity: Time: O(n), Space: O(1)
1 | class Solution(object): |
438. Find All Anagrams in a String
Description
Given two strings s and p, return an array of all the start indices of p‘s anagrams in s. You may return the answer in any order.
Click to view full description
Example 1:
- Input:
s = "cbaebabacd", p = "abc" - Output:
[0,6]
Example 2:
- Input:
s = "abab", p = "ab" - Output:
[0,1,2]
Solution
Complexity: Time: O(n), Space: O(1)
1 | class Solution: |
Top Interview 150 补充
209. Minimum Size Subarray Sum
Description
Given an array of positive integers nums and a positive integer target, return the minimal length of a subarray of nums such that the sum of the subarray is at least target. If there isn’t one, return 0 instead.
Click to view full description
Example 1:
- Input:
nums = [2, 3, 1, 2, 4, 3], target = 7 - Output:
2
Example 2:
- Input:
nums = [1, 4, 4], target = 4 - Output:
1
Solution
Idea: 使用滑动窗口。当窗口内的和大于等于 target 时,记录长度,并尝试缩小窗口,即 i 向右移动。缩小到不满足条件时,再尝试扩大窗口,即 j 向右移动。
Complexity: Time: O(n), Space: O(1)
1 | class Solution(object): |


