Skip to content

Commit c6dcd7e

Browse files
committed
add some solution
1 parent e09ac57 commit c6dcd7e

File tree

5 files changed

+75
-6
lines changed

5 files changed

+75
-6
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package leetcode._146_;
2+
3+
import java.util.HashMap;
4+
import java.util.LinkedHashMap;
5+
import java.util.Map;
6+
7+
/**
8+
* Created by zhangbo54 on 2019-07-23.
9+
*/
10+
class LRUCache2<K, V> extends LinkedHashMap<K, V> {
11+
12+
private int maxSize;
13+
14+
public LRUCache2(int maxSize) {
15+
// 第三个参数表示是否按照访问顺序组织链表
16+
super(maxSize, 0.75f, true);
17+
this.maxSize = maxSize;
18+
}
19+
20+
@Override
21+
protected boolean removeEldestEntry(Map.Entry<K, V> eldest) {
22+
return this.size() > maxSize;
23+
}
24+
}
25+
26+
/**
27+
* Your LRUCache object will be instantiated and called as such:
28+
* LRUCache obj = new LRUCache(capacity);
29+
* int param_1 = obj.get(key);
30+
* obj.put(key,value);
31+
*/

src/main/java/leetcode/_70_/Main.java

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,19 @@
66
public class Main {
77
public static void main(String[] args) {
88
Solution solution = new Solution();
9-
System.out.println( solution.climbStairs(0));
10-
System.out.println( solution.climbStairs(2));
11-
System.out.println( solution.climbStairs(3));
12-
System.out.println( solution.climbStairs(1));
13-
System.out.println( solution.climbStairs(111));
9+
long l = System.currentTimeMillis();
10+
// System.out.println( solution.climbStairs(1));
11+
System.out.println(solution.climbStairs(2));
12+
System.out.println(solution.climbStairs(3));
13+
System.out.println(solution.climbStairs(1));
14+
System.out.println("-----------------");
15+
System.out.println(solution.climbStairs(40));
16+
System.out.println(System.currentTimeMillis() - l);
1417
}
1518
}
1619

20+
// 1
21+
// 2
22+
// 3
23+
// 1
24+
// 165580141

src/main/java/leetcode/_70_/Solution.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ class Solution {
44
public int climbStairs(int n) {
55
int[] dp = new int[n + 1];
66
return this.climbStairs(dp, n);
7-
87
}
98

109
private int climbStairs(int[] dp, int n) {
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package leetcode._70_;
2+
3+
/**
4+
* Created by jacob on 2019-08-20.
5+
*/
6+
public class Solution1 {
7+
public int climbStairs(int n) {
8+
if (n <= 2) {
9+
return n;
10+
}
11+
return climbStairs(n - 1) + climbStairs(n - 2);
12+
}
13+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package leetcode._70_;
2+
3+
/**
4+
* Created by jacob on 2019-08-20.
5+
*/
6+
public class Solution2 {
7+
public int climbStairs(int n) {
8+
int[] dp = new int[n + 1];
9+
for (int i = 1; i <= n; i++) {
10+
if (i <= 2) {
11+
dp[i] = i;
12+
} else {
13+
dp[i] = dp[i - 1] + dp[i - 2];
14+
}
15+
}
16+
return dp[n];
17+
}
18+
}

0 commit comments

Comments
 (0)