Skip to content

Commit 3d5a428

Browse files
committed
fd
1 parent bbe707b commit 3d5a428

File tree

5 files changed

+56
-28
lines changed

5 files changed

+56
-28
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -384,4 +384,5 @@
384384
|669|[Trim a Binary Search Tree](https://leetcode.com/problems/trim-a-binary-search-tree/)|[Java](leetcode/solution/src/TrimABinarySearchTree.java)|100||
385385
|681|[Next Closest Time](https://leetcode.com/problems/next-closest-time/)|[Java](leetcode/solution/src/NextClosestTime.java)|80|这题多做几遍|
386386
|684|[Redundant Connection](https://leetcode.com/problems/redundant-connection/)| [Java](leetcode/solution/src/RedundantConnection.java)|||
387-
|771|[Jewels and Stones](https://leetcode.com/problems/jewels-and-stones/)|[Java](leetcode/solution/src/JewelsAndStones.java)|100||
387+
|771|[Jewels and Stones](https://leetcode.com/problems/jewels-and-stones/)|[Java](leetcode/solution/src/JewelsAndStones.java)|100||
388+
|904|[Fruit Into Baskets](https://leetcode.com/problems/fruit-into-baskets/)|[Java](leetcode/solution/src/FruitIntoBaskets.java)|90||
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import java.util.HashMap;
2+
3+
/**
4+
* 这题和sliding window较类似
5+
* 意思是给定一个数组,找出其中的一个最长的连续子数组,只能有两个不同的数
6+
*/
7+
public class FruitIntoBaskets {
8+
9+
10+
public static int totalFruit(int[] tree) {
11+
int max = 0;
12+
HashMap<Integer, Integer> map = new HashMap<>();
13+
for (int i = 0, j = 0; j < tree.length; j++) {
14+
int value = tree[j];
15+
16+
map.put(value, map.getOrDefault(value, 0) + 1);
17+
18+
for ( ; map.size() > 2; i++) {
19+
int val = tree[i], cnt = map.get(val);
20+
if (cnt == 1) {
21+
map.remove(val);
22+
} else {
23+
map.put(val, cnt - 1);
24+
}
25+
}
26+
max = Math.max(max, j - i + 1);
27+
}
28+
29+
return max;
30+
}
31+
}

leetcode/solution/src/MinimumWindowSubstring.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
public class MinimumWindowSubstring {
22

3-
// 耗时8ms,时间复杂度O(n)
3+
// 耗时6ms,时间复杂度O(n)
44
public String minWindow(String s, String t) {
55
int[] tc = new int[256], sc = new int[256];
66
for (char c : t.toCharArray()) {

leetcode/solution/src/WordBreak.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
*/
66
public class WordBreak {
77

8-
// 耗时8ms
8+
// 耗时3ms
9+
// 典型的DP问题
910
public boolean wordBreak(String s, List<String> wordDict) {
1011
int n = s.length();
1112

leetcode/src/Main.java

Lines changed: 20 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,37 +2,32 @@
22

33
public class Main {
44

5-
// 1 5 1
6-
public void nextPermutation(int[] nums) {
7-
int i = nums.length - 1, j;
8-
for (i = nums.length - 1; i > 0; i--) {
9-
if (nums[i - 1] < nums[i]) {
10-
break;
11-
}
12-
}
13-
if (i > 0) {
14-
for (j = i; j < nums.length; j++) {
15-
if (nums[j] <= nums[i - 1]) {
16-
break;
5+
public static int totalFruit(int[] tree) {
6+
int max = 0;
7+
HashMap<Integer, Integer> map = new HashMap<>();
8+
for (int i = 0, j = 0; j < tree.length; j++) {
9+
int value = tree[j];
10+
11+
map.put(value, map.getOrDefault(value, 0) + 1);
12+
13+
for ( ; map.size() > 2; i++) {
14+
int val = tree[i], cnt = map.get(val);
15+
if (cnt == 1) {
16+
map.remove(val);
17+
} else {
18+
map.put(val, cnt - 1);
1719
}
1820
}
19-
swap(nums, i - 1, j - 1);
21+
max = Math.max(max, j - i + 1);
2022
}
21-
revert(nums, i, nums.length - 1);
22-
}
23-
24-
private void revert(int[] nums, int start, int end) {
25-
for ( ; start < end; start++, end--) {
26-
swap(nums, start, end);
27-
}
28-
}
2923

30-
private void swap(int[] nums, int i, int j) {
31-
int t = nums[i];
32-
nums[i] = nums[j];
33-
nums[j] = t;
24+
return max;
3425
}
3526

3627
public static void main(String[] args) {
28+
int n = totalFruit(new int[]{
29+
1, 2, 1
30+
});
31+
System.out.println(n);
3732
}
3833
}

0 commit comments

Comments
 (0)