Skip to content

Commit 2850c15

Browse files
committed
20190117
1 parent cd5790d commit 2850c15

File tree

7 files changed

+253
-14
lines changed

7 files changed

+253
-14
lines changed

code/lc105.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* 难度:Medium
66
* 分类:Array, Tree, Depth-first Search
77
* 思路:通过递归的方式,找左节点和右节点
8-
* Tips:思路记一下,自己想不起来
8+
* Tips:思路记一下,自己想不起来。递归的方法,每次把inorder数组分为两半,设置一个pre_index,每次根据pre_index建立节点,向下递归。
99
*/
1010
public class lc105 {
1111
public static class TreeNode {

code/lc322.java

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package code;
2+
/*
3+
* 322. Coin Change
4+
* 题意:不同面额零钱组合成总值,用的零钱数最少
5+
* 难度:Medium
6+
* 分类:Dynamic Programming
7+
* 思路:和lc279一样的思路,注意下没解的情况
8+
* Tips:不用Set, 加一个dp[0]=0,可以直接递归出结果
9+
*/
10+
import java.util.Arrays;
11+
import java.util.HashSet;
12+
13+
public class lc322 {
14+
public static void main(String[] args) {
15+
System.out.println(coinChange(new int[]{2}, 3));
16+
}
17+
public static int coinChange(int[] coins, int amount) {
18+
if(amount==0) return 0;
19+
int[] dp = new int[amount];
20+
Arrays.fill(dp, Integer.MAX_VALUE);
21+
HashSet<Integer> s = new HashSet();
22+
for (int i = 0; i < coins.length ; i++) {
23+
s.add(coins[i]);
24+
}
25+
for (int i = 0; i < amount ; i++) {
26+
if(s.contains(i+1))
27+
dp[i] = 1;
28+
else{
29+
for (int j = 0; j < coins.length ; j++) {
30+
if( i+1-coins[j]>0 && dp[i - coins[j]]!=Integer.MAX_VALUE ) { // 注意子结构没解的情况
31+
dp[i] = Math.min(dp[i - coins[j]] + 1, dp[i]);
32+
}
33+
}
34+
}
35+
}
36+
return dp[amount-1]==Integer.MAX_VALUE ? -1 : dp[amount-1]; //没解返回-1
37+
}
38+
}

code/lc337.java

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package code;
2+
3+
import java.util.HashMap;
4+
5+
/*
6+
* 337. House Robber III
7+
* 题意:该节点被抢了,那么下层节点不能被抢
8+
* 难度:Medium
9+
* 分类:Tree, Depth-first Search
10+
* 思路:递归和dp。真的经典,树的递归与dp。
11+
* Tips:https://leetcode.com/problems/house-robber-iii/discuss/79330/Step-by-step-tackling-of-the-problem
12+
* 解答给了三种方法,递归,记忆递归,dp
13+
* 多理解一下树的dp,核心是返回数组而不是一个数字
14+
*/
15+
public class lc337 {
16+
public class TreeNode {
17+
int val;
18+
TreeNode left;
19+
TreeNode right;
20+
TreeNode(int x) { val = x; }
21+
}
22+
public int rob(TreeNode root) {
23+
return helper(root, new HashMap());
24+
}
25+
26+
public int helper(TreeNode root, HashMap<TreeNode, Integer> mem){
27+
if(root==null)
28+
return 0;
29+
if(mem.containsKey(root)) //用mem去记忆一下资情况的结果,防止重复计算
30+
return mem.get(root);
31+
int val =0;
32+
if(root.left!=null){
33+
val += helper(root.left.left, mem);
34+
val += helper(root.left.right, mem);
35+
}
36+
if(root.right!=null){
37+
val += helper(root.right.left, mem);
38+
val += helper(root.right.right, mem);
39+
}
40+
int res = Math.max(root.val+val, helper(root.left, mem)+helper(root.right, mem));
41+
mem.put(root, res);
42+
return res;
43+
}
44+
45+
public int rob2(TreeNode root) {
46+
return Math.max(helper2(root)[0], helper2(root)[1]);
47+
}
48+
public int[] helper2(TreeNode root){
49+
int[] res = new int[2];
50+
if(root==null) return res;
51+
int[] left = helper2(root.left);
52+
int[] right = helper2(root.right);
53+
res[0] = root.val + left[1] + right[1]; // res[0] 表示该节点被抢的最大结果,下层节点不能被抢
54+
res[1] = Math.max(left[0], left[1]) + Math.max(right[0], right[1]); // res[1] 表示该节点不抢的最大结果。注意不抢该节点,下层节点可能被抢,也可能不被抢,取大的。
55+
return res;
56+
}
57+
}

code/lc338.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package code;
2+
/*
3+
* 338. Counting Bits
4+
* 题意:0~n数字上1的个数
5+
* 难度:Medium
6+
* 分类:Dynamic Programming, Bit Maniputation
7+
* 思路:把前几个数的二进制写出来,就很容易看出来dp公式,前边的值+1即可
8+
* Tips:注意细节,边界情况
9+
*/
10+
public class lc338 {
11+
public static void main(String[] args) {
12+
int[] res = countBits(0);
13+
for (int i = 0; i < res.length ; i++) {
14+
System.out.print(res[i]);
15+
System.out.print(" ");
16+
}
17+
}
18+
public static int[] countBits(int num) {
19+
int[] dp = new int[num+1];
20+
int i = 1;
21+
while(i<=num) {
22+
dp[i] = 1;
23+
int max = i;
24+
for (int j = 0; j<max && i<=num ; j++) {
25+
dp[i] = 1 + dp[j];
26+
i++;
27+
}
28+
}
29+
return dp;
30+
}
31+
}

code/lc347.java

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package code;
2+
/*
3+
* 347. Top K Frequent Elements
4+
* 题意:找出数组中出现次数最大的k个元素
5+
* 难度:Medium
6+
* 分类:Hash Table, Heap
7+
* 思路:放入hashmap计数是基本思路,后续可以用桶排序的方法,时间复杂度为O(n)。若用优先队列,则时间复杂度为nlg(k)。
8+
* Tips:第二部巧妙的用了桶排序,避免了比较排序的复杂度
9+
*/
10+
import java.util.*;
11+
12+
public class lc347 {
13+
public static void main(String[] args) {
14+
int[] nums = {1,1,1,2,2,3};
15+
int k = 2;
16+
System.out.println(topKFrequent(nums,2));
17+
}
18+
public static List<Integer> topKFrequent(int[] nums, int k) {
19+
HashMap<Integer, Integer> hm = new HashMap();
20+
TreeMap<Integer, ArrayList<Integer>> tm = new TreeMap();
21+
List<Integer> res = new ArrayList<>();
22+
for (int i = 0; i < nums.length ; i++) { //放入hashmap计数
23+
hm.put(nums[i], hm.getOrDefault(nums[i], 0)+1);
24+
}
25+
for( int i : hm.keySet() ){ //key,value反转,放入treemap
26+
int freq = hm.get(i);
27+
if(tm.containsKey(freq))
28+
tm.get(freq).add(i);
29+
else {
30+
tm.put(freq, new ArrayList<>());
31+
tm.get(freq).add(i);
32+
}
33+
}
34+
while(res.size()<k) {
35+
List ls = tm.pollLastEntry().getValue(); //pollLastEntry将最后一个弹出,而LastEntry只是查看
36+
res.addAll(ls);
37+
}
38+
return res;
39+
}
40+
41+
public List<Integer> topKFrequent2(int[] nums, int k) {
42+
List<Integer>[] bucket = new List[nums.length + 1];
43+
Map<Integer, Integer> frequencyMap = new HashMap<Integer, Integer>();
44+
for (int n : nums) {
45+
frequencyMap.put(n, frequencyMap.getOrDefault(n, 0) + 1);
46+
}
47+
for (int key : frequencyMap.keySet()) { //桶排序,开辟一个nums.length+1的数组,因为一个数出现次数取值为[0~nums.length]
48+
int frequency = frequencyMap.get(key);
49+
if (bucket[frequency] == null) {
50+
bucket[frequency] = new ArrayList<>();
51+
}
52+
bucket[frequency].add(key);
53+
}
54+
List<Integer> res = new ArrayList<>();
55+
for (int pos = bucket.length - 1; pos >= 0 && res.size() < k; pos--) {
56+
if (bucket[pos] != null) {
57+
res.addAll(bucket[pos]);
58+
}
59+
}
60+
return res;
61+
}
62+
}

code/lc438.java

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package code;
2+
/*
3+
* 438. Find All Anagrams in a String
4+
* 题意:匹配相同字符组成的串
5+
* 难度:Easy
6+
* 分类:Hash Table
7+
* 思路:滑动窗口,O(n)时间
8+
* Tips:滑动窗口解法 https://leetcode.com/problems/find-all-anagrams-in-a-string/discuss/92007/Sliding-Window-algorithm-template-to-solve-all-the-Leetcode-substring-search-problem
9+
* lc76类似
10+
*/
11+
import java.util.ArrayList;
12+
import java.util.HashMap;
13+
import java.util.List;
14+
15+
public class lc438 {
16+
public static void main(String[] args) {
17+
System.out.println(findAnagrams("cbaebabacd", "abc"));
18+
}
19+
public static List<Integer> findAnagrams(String s, String p) {
20+
List<Integer> ls = new ArrayList<>();
21+
if(s.length()<p.length())
22+
return ls;
23+
HashMap<Character, Integer> hm = new HashMap();
24+
for (int i = 0; i < p.length() ; i++) {
25+
hm.put(p.charAt(i), hm.getOrDefault(p.charAt(i), 0)+1);
26+
}
27+
int right = 0, left = 0, count = 0;
28+
while(right<s.length()){
29+
char ch_r = s.charAt(right);
30+
char ch_l = s.charAt(left);
31+
if(hm.containsKey(ch_r)){
32+
hm.put(ch_r, hm.getOrDefault(ch_r,0)-1);
33+
if(hm.get(ch_r)>=0) //注意count++的条件,多余的相同字符不用++了
34+
count++;
35+
}
36+
if(count==p.length())
37+
ls.add(left);
38+
if(right-left+1==p.length()){ //左指针需要右移
39+
if(hm.containsKey(ch_l)){
40+
hm.put(ch_l, hm.get(ch_l)+1);
41+
if(hm.get(ch_l)>0) //count--的条件
42+
count--;
43+
}
44+
left++;
45+
}
46+
right++;
47+
}
48+
return ls;
49+
}
50+
}

readme.md

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -93,29 +93,30 @@ Welcome to improve this project with me.*
9393
| 236 | [Lowest Common Ancestor of a Binary Tree](https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tree/) |Medium| [Java](./code/lc236.java)
9494
| 238 | [Product of Array Except Self](https://leetcode.com/problems/product-of-array-except-self/) |Medium| [Java](./code/lc238.java)
9595
| 239 | [Sliding Window Maximum](https://leetcode.com/problems/sliding-window-maximum/) | Hard | [Java](./code/lc239.java)
96-
| 240 | [Search a 2D Matrix II](https://leetcode.com/problems/search-a-2d-matrix-ii/) |Medium| [Java]
97-
| 279 | [Perfect Squares](https://leetcode.com/problems/perfect-squares/) |Medium| [Java]
98-
| 283 | [Move Zeroes](https://leetcode.com/problems/move-zeroes/)| Easy | [Java]
99-
| 287 | [Find the Duplicate Number](https://leetcode.com/problems/find-the-duplicate-number/) | Hard | [Java]
96+
| 240 | [Search a 2D Matrix II](https://leetcode.com/problems/search-a-2d-matrix-ii/) |Medium| [Java](./code/lc240.java)
97+
| 279 | [Perfect Squares](https://leetcode.com/problems/perfect-squares/) |Medium| [Java](./code/lc279.java)
98+
| 283 | [Move Zeroes](https://leetcode.com/problems/move-zeroes/)| Easy | [Java](./code/lc283.java)
99+
| 287 | [Find the Duplicate Number](https://leetcode.com/problems/find-the-duplicate-number/) | Hard | [Java](./code/lc287.java)
100100
| 297 | [Serialize and Deserialize Binary Tree](https://leetcode.com/problems/serialize-and-deserialize-binary-tree/) | Hard | [Java](./code/lc297.java)
101-
| 300 | [Longest Increasing Subsequence](https://leetcode.com/problems/longest-increasing-subsequence/) |Medium| [Java]
102-
| 309 | [Best Time to Buy and Sell Stock with Cooldown](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-with-cooldown/) |Medium| [Java]
103-
| 312 | [Burst Balloons](https://leetcode.com/problems/burst-balloons/) | Hard | [Java]
104-
| 322 | [Coin Change](https://leetcode.com/problems/coin-change/)|Medium| [Java]
105-
| 337 | [House Robber III](https://leetcode.com/problems/house-robber-iii/) |Medium| [Java]
106-
| 338 | [Counting Bits](https://leetcode.com/problems/counting-bits/) |Medium| [Java]
107-
| 347 | [Top K Frequent Elements](https://leetcode.com/problems/top-k-frequent-elements/) |Medium| [Java]
101+
| 300 | [Longest Increasing Subsequence](https://leetcode.com/problems/longest-increasing-subsequence/) |Medium| [Java](./code/lc300.java)
102+
| 301 | [Remove Invalid Parentheses](https://leetcode.com/problems/remove-invalid-parentheses/) |Hard| [Java](./code/lc301.java)
103+
| 309 | [Best Time to Buy and Sell Stock with Cooldown](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-with-cooldown/) |Medium| [Java](./code/lc309.java)
104+
| 312 | [Burst Balloons](https://leetcode.com/problems/burst-balloons/) | Hard | [Java](./code/lc312.java)
105+
| 322 | [Coin Change](https://leetcode.com/problems/coin-change/)|Medium| [Java](./code/lc322.java)
106+
| 337 | [House Robber III](https://leetcode.com/problems/house-robber-iii/) |Medium| [Java](./code/lc337.java)
107+
| 338 | [Counting Bits](https://leetcode.com/problems/counting-bits/) |Medium| [Java](./code/lc338.java)
108+
| 347 | [Top K Frequent Elements](https://leetcode.com/problems/top-k-frequent-elements/) |Medium| [Java](./code/lc347.java)
108109
| 394 | [Decode String](https://leetcode.com/problems/decode-string/) |Medium| [Java]
109110
| 406 | [Queue Reconstruction by Height](https://leetcode.com/problems/queue-reconstruction-by-height/) |Medium| [Java]
110111
| 416 | [Partition Equal Subset Sum](https://leetcode.com/problems/partition-equal-subset-sum/) |Medium| [Java]
111112
| 437 | [Path Sum III](https://leetcode.com/problems/path-sum-iii/) | Easy |[Java]
112-
| 438 | [Find All Anagrams in a String](https://leetcode.com/problems/find-all-anagrams-in-a-string/) | Easy |[Java]
113+
| 438 | [Find All Anagrams in a String](https://leetcode.com/problems/find-all-anagrams-in-a-string/) | Easy |[Java](./code/lc438.java)
113114
| 448 | [Find All Numbers Disappeared in an Array](https://leetcode.com/problems/find-all-numbers-disappeared-in-an-array/) | Easy | [Java]
114115
| 461 | [Hamming Distance](https://leetcode.com/problems/hamming-distance/) | Easy |[Java]
115116
| 494 | [Target Sum](https://leetcode.com/problems/target-sum/) |Medium|[Java]
116117
| 538 | [Convert BST to Greater Tree](https://leetcode.com/problems/convert-bst-to-greater-tree/description/) | Easy |[Java]
117118
| 543 | [Diameter of Binary Tree](https://leetcode.com/problems/diameter-of-binary-tree/description/) | Easy | [Java]
118-
| 560 | [Subarray Sum Equals K](https://leetcode.com/problems/subarray-sum-equals-k) |Medium| [Java]
119+
| 560 | [Subarray Sum Equals K](https://leetcode.com/problems/subarray-sum-equals-k) |Medium| [Java](./code/lc560.java)
119120
| 572 | [Subtree of Another Tree](https://leetcode.com/problems/subtree-of-another-tree/description/) | Easy | [Java]
120121
| 581 | [Shortest Unsorted Continuous Subarray](https://leetcode.com/problems/shortest-unsorted-continuous-subarray/description/) | Easy | [Java]
121122
| 617 | [Merge Two Binary Trees](https://leetcode.com/problems/merge-two-binary-trees/description/) | Easy | [Java]

0 commit comments

Comments
 (0)