Skip to content

Commit 8493a3d

Browse files
committed
fd
1 parent ba976d6 commit 8493a3d

File tree

4 files changed

+137
-73
lines changed

4 files changed

+137
-73
lines changed

leetcode/solution/src/InorderSuccessorInBST.java

Lines changed: 16 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -29,32 +29,8 @@ public TreeNode inorderSuccessor(TreeNode root, TreeNode p) {
2929
return null;
3030
}
3131

32+
// 耗时2ms,简单的递归写法,更容易理解
3233
/**
33-
* p的下一个节点一定是比p大的,所以如果p没有右子树,则结果是p的父节点,如果有右子树则是右子树中最小的节点。
34-
* 所以这里遍历时当p的值小于当前节点,则当前节点可作为备选,同时往左走。如果在遍历过程中遇到仍然比p大的,说明更接近p,则更新备选。
35-
* 如果遇到比p小的,就往右走
36-
* <p>
37-
* 有两点要注意,
38-
* 1, 首先res初始要为null,一个节点时,或root为null时,或p为最大节点时,res都没机会赋值
39-
* 2, 当root迭代到等于p时,走哪个分支呢,为什么选root = root.right,假如root.right为空,则之前的res即可,否则
40-
* 下一个迭代肯定走到root.val > p.val分支中,更新res。
41-
*/
42-
// 耗时4ms
43-
public TreeNode inorderSuccessor2(TreeNode root, TreeNode p) {
44-
TreeNode res = null;
45-
while (root != null) {
46-
if (root.val > p.val) {
47-
res = root;
48-
root = root.left;
49-
} else {
50-
root = root.right;
51-
}
52-
}
53-
return res;
54-
}
55-
56-
// 耗时2ms,简单的递归写法
57-
/*
5834
public TreeNode inorderSuccessor(TreeNode root, TreeNode p) {
5935
if (root == null) {
6036
return null;
@@ -79,6 +55,21 @@ public TreeNode predecessor(TreeNode root, TreeNode p) {
7955
}
8056
}*/
8157

58+
// 给上面的递归换成迭代写法
59+
// 耗时4ms
60+
public TreeNode inorderSuccessor2(TreeNode root, TreeNode p) {
61+
TreeNode res = null;
62+
while (root != null) {
63+
if (root.val > p.val) {
64+
res = root;
65+
root = root.left;
66+
} else {
67+
root = root.right;
68+
}
69+
}
70+
return res;
71+
}
72+
8273
/**
8374
* http://www.geeksforgeeks.org/?p=9999
8475
* 给定Node,求其successor,步骤如下:

leetcode/solution/src/LRUCache.java

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import java.util.HashMap;
2+
3+
public class LRUCache {
4+
5+
private Node mHead;
6+
private HashMap<Integer, Node> mMap;
7+
private int mCapacity;
8+
9+
public LRUCache(int capacity) {
10+
mMap = new HashMap<>();
11+
mCapacity = capacity;
12+
mHead = new Node(0, 0);
13+
}
14+
15+
public int get(int key) {
16+
if (!mMap.containsKey(key)) {
17+
return -1;
18+
}
19+
Node node = mMap.get(key);
20+
node.remove();
21+
node.add(mHead);
22+
return node.val;
23+
}
24+
25+
public void put(int key, int value) {
26+
Node node = mMap.get(key);
27+
if (node == null) {
28+
node = new Node(key, value);
29+
mMap.put(key, node);
30+
} else {
31+
node.val = value;
32+
node.remove();
33+
}
34+
node.add(mHead);
35+
if (mMap.size() > mCapacity) {
36+
Node prev = mHead.prev;
37+
prev.remove();
38+
mMap.remove(prev.key);
39+
}
40+
}
41+
42+
class Node {
43+
Node prev, next;
44+
int key, val;
45+
46+
Node(int key, int val) {
47+
this.key = key;
48+
this.val = val;
49+
prev = next = this;
50+
}
51+
52+
void remove() {
53+
prev.next = next;
54+
next.prev = prev;
55+
}
56+
57+
void add(Node head) {
58+
next = head.next;
59+
head.next.prev = this;
60+
prev = head;
61+
head.next = this;
62+
}
63+
}
64+
}

leetcode/solution/src/TwoSum.java

Lines changed: 3 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,16 @@
11
import java.util.HashMap;
22

3-
/**
4-
* https://leetcode.com/articles/two-sum/
5-
*/
63
public class TwoSum {
74

85
/**
9-
* 如果符合条件的不止一组呢?则找到一组就从map删除一组
10-
* 要注意排除index != i
11-
*/
12-
public int[] twoSum(int[] nums, int target) {
13-
HashMap<Integer, Integer> map = new HashMap<>();
14-
for (int i = 0; i < nums.length; i++) {
15-
map.put(nums[i], i);
16-
}
17-
for (int i = 0; i < nums.length; i++) {
18-
Integer index = map.get(target - nums[i]);
19-
if (index != null && index != i) {
20-
return new int[] {
21-
i, index
22-
};
23-
}
24-
}
25-
return null;
26-
}
27-
28-
/**
29-
* 只循环一轮
6+
* one pass
307
* 要注意map.put要放在for末尾,对于case[3, 3], target=6的情况,如果放在开头会覆盖第一个3
318
*/
329
public int[] twoSum2(int[] nums, int target) {
3310
HashMap<Integer, Integer> map = new HashMap<>();
3411
for (int i = 0; i < nums.length; i++) {
35-
int k = map.getOrDefault(target - nums[i], -1);
36-
if (k >= 0 && k != i) {
37-
return new int[] {i, k};
12+
if (map.containsKey(target - nums[i])) {
13+
return new int[] {map.get(target - nums[i]), i};
3814
}
3915
map.put(nums[i], i);
4016
}

leetcode/src/Main.java

Lines changed: 54 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,37 +2,70 @@
22

33
public class Main {
44

5-
class TwoSum {
5+
class LRUCache {
66

7-
HashMap<Integer, Integer> map;
7+
class Node {
8+
Node prev, next;
9+
int key, val;
810

9-
/** Initialize your data structure here. */
10-
public TwoSum() {
11-
map = new HashMap<>();
11+
Node(int key, int val) {
12+
this.key = key;
13+
this.val = val;
14+
prev = next = this;
15+
}
16+
17+
void remove() {
18+
prev.next = next;
19+
next.prev = prev;
20+
}
21+
22+
void add(Node head) {
23+
next = head.next;
24+
head.next.prev = this;
25+
prev = head;
26+
head.next = this;
27+
}
1228
}
1329

14-
/** Add the number to an internal data structure.. */
15-
public void add(int number) {
16-
map.put(number, map.getOrDefault(number, 0) + 1);
30+
private Node mHead;
31+
private HashMap<Integer, Node> mMap;
32+
private int mCapacity;
33+
34+
public LRUCache(int capacity) {
35+
mMap = new HashMap<>();
36+
mCapacity = capacity;
37+
mHead = new Node(0, 0);
1738
}
1839

19-
/** Find if there exists any pair of numbers which sum is equal to the value. */
20-
public boolean find(int value) {
21-
for (int k : map.keySet()) {
22-
int n = map.getOrDefault(value - k, -1);
23-
if (value - k != k && n > 0) {
24-
return true;
25-
}
26-
if (value - k == k && n > 1) {
27-
return true;
28-
}
40+
public int get(int key) {
41+
if (!mMap.containsKey(key)) {
42+
return -1;
43+
}
44+
Node node = mMap.get(key);
45+
node.remove();
46+
node.add(mHead);
47+
return node.val;
48+
}
49+
50+
public void put(int key, int value) {
51+
Node node = mMap.get(key);
52+
if (node == null) {
53+
node = new Node(key, value);
54+
mMap.put(key, node);
55+
} else {
56+
node.val = value;
57+
node.remove();
58+
}
59+
node.add(mHead);
60+
if (mMap.size() > mCapacity) {
61+
Node prev = mHead.prev;
62+
prev.remove();
63+
mMap.remove(prev.key);
2964
}
30-
return false;
3165
}
3266
}
3367

3468
public static void main(String[] args) {
35-
int[] res = twoSum(new int[]{3,2,3}, 6);
36-
System.out.println(String.format("%d - %d", res[0], res[1]));
69+
3770
}
3871
}

0 commit comments

Comments
 (0)