Skip to content

Commit b9d51c7

Browse files
author
Botao Xiao
committed
[Function add]1. Add leetcode solutions.
1 parent 53ee678 commit b9d51c7

6 files changed

+585
-2
lines changed

leetcode/11. Container With Most Water.md

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,4 +61,22 @@ class Solution {
6161
return max;
6262
}
6363
}
64-
```
64+
```
65+
66+
### Third Time
67+
* Method 1: Two pointer
68+
```Java
69+
class Solution {
70+
public int maxArea(int[] height) {
71+
if(height == null || height.length == 0) return 0;
72+
int slow = 0, fast = height.length - 1;
73+
int max = 0;
74+
while(slow < fast){
75+
max = Math.max(max, (fast - slow) * Math.min(height[fast], height[slow]));
76+
if(height[fast] < height[slow]) fast--;
77+
else slow++;
78+
}
79+
return max;
80+
}
81+
}
82+
```

leetcode/146. LRU Cache.md

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
## 146. LRU Cache
2+
3+
### Question
4+
Design and implement a data structure for Least Recently Used (LRU) cache. It should support the following operations: get and put.
5+
6+
get(key) - Get the value (will always be positive) of the key if the key exists in the cache, otherwise return -1.
7+
put(key, value) - Set or insert the value if the key is not already present. When the cache reached its capacity, it should invalidate the least recently used item before inserting a new item.
8+
9+
Follow up:
10+
* Could you do both operations in O(1) time complexity?
11+
12+
```
13+
Example:
14+
15+
LRUCache cache = new LRUCache( 2 /* capacity */ );
16+
17+
cache.put(1, 1);
18+
cache.put(2, 2);
19+
cache.get(1); // returns 1
20+
cache.put(3, 3); // evicts key 2
21+
cache.get(2); // returns -1 (not found)
22+
cache.put(4, 4); // evicts key 1
23+
cache.get(1); // returns -1 (not found)
24+
cache.get(3); // returns 3
25+
cache.get(4); // returns 4
26+
```
27+
28+
### Solution
29+
* Method 1: HashMap + Bidirectional list
30+
```Java
31+
class LRUCache {
32+
private int capacity;
33+
private Map<Integer, DequeNode> map;
34+
private DequeNode dummy;
35+
private DequeNode tail;
36+
private int size;
37+
private static class DequeNode{
38+
public int key;
39+
public int val;
40+
public DequeNode pre, next;
41+
public DequeNode(int key, int val){
42+
this.key = key;
43+
this.val = val;
44+
}
45+
}
46+
public LRUCache(int capacity) {
47+
this.capacity = capacity;
48+
this.map = new HashMap<>();
49+
this.size = 0;
50+
this.dummy = new DequeNode(-1, -1);
51+
}
52+
53+
private void insertHead(DequeNode cur){
54+
if(cur.pre != null){
55+
cur.pre.next = cur.next; //remove current node from the list.
56+
if(cur.next != null)
57+
cur.next.pre = cur.pre;
58+
}
59+
DequeNode originalHead = dummy.next; //insert current node to the head of the list.
60+
cur.pre = dummy;
61+
dummy.next = cur;
62+
cur.next = originalHead;
63+
if(originalHead != null){
64+
originalHead.pre = cur;
65+
}
66+
}
67+
68+
public int get(int key) {
69+
if(!map.containsKey(key)) return -1;
70+
DequeNode cur = map.get(key);
71+
if(cur == tail && size != 1){
72+
this.tail = this.tail.pre;
73+
this.tail.next = null;
74+
}
75+
insertHead(cur);
76+
return cur.val;
77+
}
78+
79+
public void put(int key, int value) {
80+
if(map.containsKey(key)){
81+
DequeNode cur = map.get(key);
82+
if(cur == tail && size != 1){
83+
tail = cur.pre;
84+
}
85+
cur.val = value;
86+
insertHead(cur);
87+
}else{
88+
DequeNode node = new DequeNode(key, value);
89+
this.map.put(key, node);
90+
if(size + 1 <= this.capacity){
91+
if(size + 1 == 1){
92+
this.tail = node;
93+
}
94+
insertHead(node);
95+
size++;
96+
}else{
97+
// Need to remove the last node and insert current one.
98+
insertHead(node);
99+
DequeNode originalTail = this.tail;
100+
map.remove(originalTail.key);
101+
this.tail = this.tail.pre;
102+
this.tail.next = null;
103+
}
104+
}
105+
}
106+
}
107+
108+
/**
109+
* Your LRUCache object will be instantiated and called as such:
110+
* LRUCache obj = new LRUCache(capacity);
111+
* int param_1 = obj.get(key);
112+
* obj.put(key,value);
113+
*/
114+
```

leetcode/167. Two Sum II - Input array is sorted.md

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,4 +83,21 @@ class Solution {
8383
return new int[]{low + 1, high + 1};
8484
}
8585
}
86-
```
86+
```
87+
88+
### Third Time
89+
* Method 1: Two pointer
90+
```Java
91+
class Solution {
92+
public int[] twoSum(int[] numbers, int target) {
93+
int slow = 0, fast = numbers.length - 1;
94+
while(slow < fast){
95+
int sum = numbers[slow] + numbers[fast];
96+
if(sum == target) return new int[]{slow + 1, fast + 1};
97+
else if(sum < target) slow++;
98+
else fast--;
99+
}
100+
return new int[]{-1, -1};
101+
}
102+
}
103+
```

0 commit comments

Comments
 (0)