Skip to content

Commit ea2a15c

Browse files
author
rchen102
committed
sliding window
1 parent cf0f74d commit ea2a15c

8 files changed

+253
-43
lines changed

Java/leetcode 209.java

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,30 @@
44
class Solution {
55
public int minSubArrayLen(int s, int[] nums) {
66
if (nums == null || nums.length == 0) return 0;
7+
// helper vars
8+
int sum = 0, len = nums.length + 1;
9+
10+
// sliding window
711
int left = 0, right = 0;
8-
int sum = 0, minLen = nums.length + 1;
12+
boolean isNeedShrink = false;
913
while (right < nums.length) {
10-
sum += nums[right];
11-
if (sum >= s) {
12-
while (sum >= s) {
13-
minLen = Math.min(minLen, right - left + 1);
14-
sum -= nums[left];
15-
left++;
16-
}
17-
right++;
18-
}
19-
else {
20-
right++;
14+
int n = nums[right];
15+
right++;
16+
17+
sum += n;
18+
if (sum >= s) isNeedShrink = true;
19+
20+
while (isNeedShrink) {
21+
len = Math.min(len, right - left);
22+
23+
int toRemove = nums[left];
24+
left++;
25+
26+
sum -= toRemove;
27+
if (sum < s) isNeedShrink = false;
2128
}
2229
}
23-
if (minLen <= nums.length) return minLen;
24-
return 0;
30+
return len > nums.length ? 0 : len;
2531
}
2632
}
2733

Java/leetcode 3.java

Lines changed: 32 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,56 @@
1-
/* Solution: sliding window + set
1+
/* Solution: sliding window + Map
22
* T: O(n) S: O(1)
33
*/
44
class Solution {
55
public int lengthOfLongestSubstring(String s) {
66
if (s == null || s.length() == 0) return 0;
7-
int left = 0, right = 0; // sliding window
7+
Map<Character, Integer> map = new HashMap<>(); // 判断是否重复使用
8+
int left = 0, right = 0;
9+
boolean isNeedShrink = false;
810
int res = 0;
9-
Set<Character> set = new HashSet<>();
1011
while (right < s.length()) {
1112
char ch = s.charAt(right);
12-
if (!set.contains(ch)) {
13-
set.add(ch);
14-
res = Math.max(res, set.size());
15-
right++;
16-
} else {
17-
while (set.contains(ch)) {
18-
set.remove(s.charAt(left));
19-
left++;
20-
}
13+
right++;
14+
map.put(ch, map.getOrDefault(ch, 0) + 1);
15+
if (map.get(ch) > 1) isNeedShrink = true;
16+
17+
while (isNeedShrink) {
18+
char toRemove = s.charAt(left);
19+
left++;
20+
map.put(toRemove, map.get(toRemove) - 1);
21+
if (toRemove == ch) isNeedShrink = false;
2122
}
23+
res = Math.max(right - left, res);
2224
}
2325
return res;
2426
}
2527
}
2628

27-
/* Solution: sliding window + array -> hashmap
29+
/* Solution: sliding window + array -> hashmap 更快
2830
* T: O(n) S: O(1)
2931
*/
3032
class Solution {
3133
public int lengthOfLongestSubstring(String s) {
32-
int[] map = new int[256];
34+
if (s == null || s.length() == 0) return 0;
35+
int[] map = new int[256]; // 判断是否重复使用
3336
int left = 0, right = 0;
34-
int maxLen = 0;
37+
boolean isNeedShrink = false;
38+
int res = 0;
3539
while (right < s.length()) {
36-
if (map[s.charAt(right)] == 0) {
37-
maxLen = Math.max(right - left + 1, maxLen);
38-
map[s.charAt(right)]++;
39-
right++;
40-
}
41-
else {
42-
while (map[s.charAt(right)] != 0) {
43-
map[s.charAt(left)] = 0;
44-
left++;
45-
}
40+
char ch = s.charAt(right);
41+
right++;
42+
43+
map[ch]++;
44+
if (map[ch] > 1) isNeedShrink = true;
45+
46+
while (isNeedShrink) {
47+
char toRemove = s.charAt(left);
48+
left++;
49+
map[toRemove]--;
50+
if (toRemove == ch) isNeedShrink = false;
4651
}
52+
res = Math.max(right - left, res);
4753
}
48-
return maxLen;
54+
return res;
4955
}
5056
}

Java/leetcode 35.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ public int searchInsert(int[] nums, int target) {
66
if (nums == null || nums.length == 0) return 0;
77
int lo = 0, hi = nums.length - 1;
88
while (lo <= hi) {
9-
int mid = lo + (hi - lo) / 2;
10-
if (nums[mid] < target) lo = mid + 1;
9+
int mid = lo + ((hi - lo) >> 1);
10+
if (nums[mid] < target) lo = mid + 1;
1111
else if (nums[mid] > target) hi = mid - 1;
1212
else return mid;
13-
}
13+
}
1414
return lo;
1515
}
1616
}

Java/leetcode 39.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,30 @@
11
/* Solution: backtrace
22
*
33
*/
4+
class Solution {
5+
public List<List<Integer>> combinationSum(int[] candidates, int target) {
6+
List<List<Integer>> res = new ArrayList<>();
7+
if (candidates == null || candidates.length == 0) return res;
8+
Arrays.sort(candidates);
9+
backtrace(candidates, target, 0, new ArrayList<>(), res);
10+
return res;
11+
}
12+
13+
private void backtrace(int[] candidates, int remain, int start, List<Integer> tmp, List<List<Integer>> res) {
14+
if (remain == 0) {
15+
res.add(new ArrayList<>(tmp));
16+
return;
17+
}
18+
for (int i = start; i < candidates.length; i++) {
19+
if (candidates[i] > remain) break;
20+
tmp.add(candidates[i]);
21+
backtrace(candidates, remain - candidates[i], i, tmp, res);
22+
tmp.remove(tmp.size() - 1);
23+
}
24+
}
25+
}
26+
27+
// 未排序
428
class Solution {
529
public List<List<Integer>> combinationSum(int[] candidates, int target) {
630
List<List<Integer>> res = new ArrayList<>();

Java/leetcode 40.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,32 @@
11
/* Solution: backtrace
22
*
33
*/
4+
class Solution {
5+
public List<List<Integer>> combinationSum2(int[] candidates, int target) {
6+
List<List<Integer>> res = new ArrayList<>();
7+
if (candidates == null || candidates.length == 0) return res;
8+
Arrays.sort(candidates); // log(n)
9+
backtrace(candidates, target, 0, new ArrayList<>(), res);
10+
return res;
11+
}
12+
13+
private void backtrace(int[] candidates, int remain, int start, List<Integer> tmp, List<List<Integer>> res) {
14+
if (remain == 0) {
15+
res.add(new ArrayList<>(tmp));
16+
return;
17+
}
18+
for (int i = start; i < candidates.length; i++) {
19+
if (candidates[i] > remain) break;
20+
if (i != start && candidates[i] == candidates[i-1]) continue; // avoid duplicate solution
21+
tmp.add(candidates[i]);
22+
backtrace(candidates, remain - candidates[i], i + 1, tmp, res);
23+
tmp.remove(tmp.size() - 1);
24+
}
25+
}
26+
}
27+
28+
29+
430
class Solution {
531
public List<List<Integer>> combinationSum2(int[] candidates, int target) {
632
List<List<Integer>> res = new ArrayList<>();

Java/leetcode 438.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// Solution1: sliding window T: O(n) S: O(1)
2+
class Solution {
3+
public List<Integer> findAnagrams(String s, String p) {
4+
List<Integer> res = new ArrayList<>();
5+
if (s == null || s.length() == 0) return res;
6+
// helper variables
7+
int[] code = new int[26];
8+
for (char ch : p.toCharArray()) {
9+
code[ch-'a'] += 1;
10+
}
11+
// sliding window
12+
int windowLen = p.length();
13+
for (int i = 0; i < s.length(); i++) {
14+
char ch = s.charAt(i);
15+
code[ch-'a'] -= 1;
16+
17+
if (i >= windowLen) {
18+
char leave = s.charAt(i-windowLen);
19+
code[leave-'a'] += 1;
20+
}
21+
if (isAllZero(code)) res.add(i-windowLen+1);
22+
}
23+
return res;
24+
}
25+
26+
private boolean isAllZero(int[] code) {
27+
for (int i = 0; i < code.length; i++) {
28+
if (code[i] != 0) return false;
29+
}
30+
return true;
31+
}
32+
}

Java/leetcode 567.java

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
// Solution1: sliding window T: O(n) S: O(1)
2+
class Solution {
3+
public boolean checkInclusion(String s1, String s2) {
4+
if (s2 == null || s2.length() == 0 || s1 == null || s1.length() == 0) return false;
5+
// helper variables
6+
int[] code = new int[26];
7+
for (char ch : s1.toCharArray()) {
8+
code[ch-'a'] += 1;
9+
}
10+
11+
// sliding window
12+
int windowLen = s1.length();
13+
for (int i = 0; i < s2.length(); i++) {
14+
char ch = s2.charAt(i);
15+
code[ch-'a'] -= 1;
16+
if (i >= windowLen) {
17+
char leave = s2.charAt(i - windowLen);
18+
code[leave-'a'] += 1;
19+
}
20+
if (isAllZero(code)) return true;
21+
}
22+
return false;
23+
}
24+
25+
private boolean isAllZero(int[] code) {
26+
for (int i = 0; i < code.length; i++) {
27+
if (code[i] != 0) return false;
28+
}
29+
return true;
30+
}
31+
}
32+
33+
// Solution1: sliding window T: O(n) S: O(n)
34+
class Solution {
35+
public boolean checkInclusion(String s1, String s2) {
36+
if (s2 == null || s2.length() == 0 || s1 == null || s1.length() == 0) return false;
37+
// helper variables
38+
char[] code = new char[26];
39+
for (char ch : s1.toCharArray()) {
40+
code[ch-'a'] += 1;
41+
}
42+
String need = String.valueOf(code);
43+
code = new char[26];
44+
45+
// sliding window
46+
int left = 0, right = 0;
47+
boolean isNeedShrink = false;
48+
while (right < s2.length()) {
49+
char ch = s2.charAt(right);
50+
right++;
51+
52+
code[ch-'a'] += 1;
53+
if (right - left == s1.length()) isNeedShrink = true;
54+
55+
while (isNeedShrink) {
56+
if (need.equals(String.valueOf(code))) return true;
57+
58+
char leave = s2.charAt(left);
59+
left++;
60+
61+
code[leave - 'a'] -= 1;
62+
if (right - left < s1.length()) isNeedShrink = false;
63+
}
64+
}
65+
return false;
66+
}
67+
}
68+

Java/leetcode 76.java

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// Solution1: sliding window T: O(n) S: O(n)
2+
class Solution {
3+
public String minWindow(String s, String t) {
4+
if (s == null || s.length() == 0 || t == null || t.length() == 0) return "";
5+
6+
// helper vars
7+
Map<Character, Integer> need = new HashMap<>();
8+
Map<Character, Integer> window = new HashMap<>();
9+
int valid = 0;
10+
for (char ch : t.toCharArray()) {
11+
need.put(ch, need.getOrDefault(ch, 0) + 1);
12+
}
13+
int start = -1, len = s.length() + 1; // final result
14+
15+
// sliding window
16+
int left = 0, right = 0;
17+
boolean isNeedShrink = false;
18+
while (right < s.length()) {
19+
// 右移
20+
char c = s.charAt(right);
21+
right++;
22+
23+
if (need.containsKey(c)) {
24+
window.put(c, window.getOrDefault(c, 0) + 1);
25+
if ((int) window.get(c) == (int) need.get(c)) valid++;
26+
}
27+
if (valid == need.size()) isNeedShrink = true;
28+
29+
while (isNeedShrink) {
30+
// update result
31+
if (right - left < len) {
32+
start = left;
33+
len = right - left;
34+
}
35+
// 左移
36+
char d = s.charAt(left);
37+
left++;
38+
39+
if (need.containsKey(d)) {
40+
if ((int) window.get(d) == (int) need.get(d)) valid--;
41+
window.put(d, window.get(d) - 1);
42+
}
43+
if (valid != need.size()) isNeedShrink = false;
44+
}
45+
}
46+
return start == -1 ? "" : s.substring(start, start + len);
47+
}
48+
}

0 commit comments

Comments
 (0)