Skip to content

Commit 9b062a8

Browse files
committed
add 146 and update 0232 README
1 parent e7d446b commit 9b062a8

File tree

4 files changed

+193
-0
lines changed

4 files changed

+193
-0
lines changed

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
| [11](./algorithms/0011) | [11. 盛最多水的容器](https://leetcode-cn.com/problems/container-with-most-water/) | [Go](./algorithms/0011/main.go) | 中等 |
1717
| [14](./algorithms/0014) | [14. 最长公共前缀](https://leetcode-cn.com/problems/longest-common-prefix/) | [Go](./algorithms/0014/main.go) | 简单 |
1818
| [79](./algorithms/0079) | [79. 单词搜索](https://leetcode-cn.com/problems/word-search/) | [Go](./algorithms/0079/main.go) | 中等 |
19+
| [146](./algorithms/0146) | [146. LRU 缓存机制](https://leetcode-cn.com/problems/lru-cache/) | [Go](./algorithms/0146/main.go) | 中等 |
20+
| [232](./algorithms/0232) | [232. 用栈实现队列](https://leetcode-cn.com/problems/implement-queue-using-stacks/) | [Go](./algorithms/0232/main.go) | 简单 |
1921
| [263](./algorithms/0263) | [263. 丑数](https://leetcode-cn.com/problems/ugly-number/) | [Go](./algorithms/0263/main.go) | 简单 |
2022
| [offer47](./algorithms/offer47) | [剑指 Offer 47. 礼物的最大价值](https://leetcode-cn.com/problems/li-wu-de-zui-da-jie-zhi-lcof/) | [Go](./algorithms/offer47/main.go) | 中等 |
2123

algorithms/0146/README.md

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#### [146. LRU 缓存机制](https://leetcode-cn.com/problems/lru-cache/)
2+
3+
难度中等
4+
5+
运用你所掌握的数据结构,设计和实现一个 [LRU (最近最少使用) 缓存机制](https://baike.baidu.com/item/LRU)
6+
7+
实现 `LRUCache` 类:
8+
9+
- `LRUCache(int capacity)` 以正整数作为容量 `capacity` 初始化 LRU 缓存
10+
- `int get(int key)` 如果关键字 `key` 存在于缓存中,则返回关键字的值,否则返回 `-1`
11+
- `void put(int key, int value)` 如果关键字已经存在,则变更其数据值;如果关键字不存在,则插入该组「关键字-值」。当缓存容量达到上限时,它应该在写入新数据之前删除最久未使用的数据值,从而为新的数据值留出空间。
12+
13+
14+
15+
**进阶**:你是否可以在 `O(1)` 时间复杂度内完成这两种操作?
16+
17+
18+
19+
**示例:**
20+
21+
```
22+
输入
23+
["LRUCache", "put", "put", "get", "put", "get", "put", "get", "get", "get"]
24+
[[2], [1, 1], [2, 2], [1], [3, 3], [2], [4, 4], [1], [3], [4]]
25+
输出
26+
[null, null, null, 1, null, -1, null, -1, 3, 4]
27+
28+
解释
29+
LRUCache lRUCache = new LRUCache(2);
30+
lRUCache.put(1, 1); // 缓存是 {1=1}
31+
lRUCache.put(2, 2); // 缓存是 {1=1, 2=2}
32+
lRUCache.get(1); // 返回 1
33+
lRUCache.put(3, 3); // 该操作会使得关键字 2 作废,缓存是 {1=1, 3=3}
34+
lRUCache.get(2); // 返回 -1 (未找到)
35+
lRUCache.put(4, 4); // 该操作会使得关键字 1 作废,缓存是 {4=4, 3=3}
36+
lRUCache.get(1); // 返回 -1 (未找到)
37+
lRUCache.get(3); // 返回 3
38+
lRUCache.get(4); // 返回 4
39+
```
40+
41+
42+
43+
**提示:**
44+
45+
- `1 <= capacity <= 3000`
46+
- `0 <= key <= 10000`
47+
- `0 <= value <= 105`
48+
- 最多调用 `2 * 105``get``put`

algorithms/0146/main.go

+88
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
package _0146
2+
3+
type LRUCache struct {
4+
cap int
5+
m map[int]*Node
6+
head *Node
7+
tail *Node
8+
}
9+
10+
type Node struct {
11+
key int
12+
value int
13+
prev *Node
14+
next *Node
15+
}
16+
17+
func Constructor(capacity int) LRUCache {
18+
c := LRUCache{
19+
cap: capacity,
20+
m: map[int]*Node{},
21+
head: &Node{},
22+
tail: &Node{},
23+
}
24+
c.head.next = c.tail
25+
c.tail.prev = c.head
26+
return c
27+
}
28+
29+
func (this *LRUCache) Get(key int) int {
30+
node, ok := this.m[key]
31+
if !ok {
32+
return -1
33+
}
34+
this.addToFirst(node)
35+
return node.value
36+
37+
}
38+
func (this *LRUCache) addToFirst(node *Node) {
39+
if this.head.next == node {
40+
return
41+
}
42+
firstOld := this.head.next
43+
prev := node.prev
44+
next := node.next
45+
46+
if prev != nil {
47+
prev.next = next
48+
}
49+
if next != nil {
50+
next.prev = prev
51+
}
52+
53+
firstOld.prev = node
54+
node.next = firstOld
55+
node.prev = this.head
56+
this.head.next = node
57+
this.m[node.key] = node
58+
}
59+
60+
func (this *LRUCache) removeLast() {
61+
last := this.tail.prev
62+
delete(this.m, last.key)
63+
prev := last.prev
64+
this.tail.prev = prev
65+
prev.next = this.tail
66+
}
67+
68+
func (this *LRUCache) Put(key int, value int) {
69+
node, ok := this.m[key]
70+
if ok {
71+
node.value = value
72+
} else {
73+
node = &Node{key: key, value: value}
74+
}
75+
this.addToFirst(node)
76+
this.m[key] = node
77+
78+
if len(this.m) > this.cap {
79+
this.removeLast()
80+
}
81+
}
82+
83+
/**
84+
* Your LRUCache object will be instantiated and called as such:
85+
* obj := Constructor(capacity);
86+
* param_1 := obj.Get(key);
87+
* obj.Put(key,value);
88+
*/

algorithms/0232/README.md

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
#### [232. 用栈实现队列](https://leetcode-cn.com/problems/implement-queue-using-stacks/)
2+
3+
难度简单
4+
5+
请你仅使用两个栈实现先入先出队列。队列应当支持一般队列支持的所有操作(`push``pop``peek``empty`):
6+
7+
实现 `MyQueue` 类:
8+
9+
- `void push(int x)` 将元素 x 推到队列的末尾
10+
- `int pop()` 从队列的开头移除并返回元素
11+
- `int peek()` 返回队列开头的元素
12+
- `boolean empty()` 如果队列为空,返回 `true` ;否则,返回 `false`
13+
14+
15+
16+
**说明:**
17+
18+
- 你只能使用标准的栈操作 —— 也就是只有 `push to top`, `peek/pop from top`, `size`, 和 `is empty` 操作是合法的。
19+
- 你所使用的语言也许不支持栈。你可以使用 list 或者 deque(双端队列)来模拟一个栈,只要是标准的栈操作即可。
20+
21+
22+
23+
**进阶:**
24+
25+
- 你能否实现每个操作均摊时间复杂度为 `O(1)` 的队列?换句话说,执行 `n` 个操作的总时间复杂度为 `O(n)` ,即使其中一个操作可能花费较长时间。
26+
27+
28+
29+
**示例:**
30+
31+
```
32+
输入:
33+
["MyQueue", "push", "push", "peek", "pop", "empty"]
34+
[[], [1], [2], [], [], []]
35+
输出:
36+
[null, null, null, 1, 1, false]
37+
38+
解释:
39+
MyQueue myQueue = new MyQueue();
40+
myQueue.push(1); // queue is: [1]
41+
myQueue.push(2); // queue is: [1, 2] (leftmost is front of the queue)
42+
myQueue.peek(); // return 1
43+
myQueue.pop(); // return 1, queue is [2]
44+
myQueue.empty(); // return false
45+
```
46+
47+
48+
49+
50+
51+
**提示:**
52+
53+
- `1 <= x <= 9`
54+
- 最多调用 `100``push``pop``peek``empty`
55+
- 假设所有操作都是有效的 (例如,一个空的队列不会调用 `pop` 或者 `peek` 操作)

0 commit comments

Comments
 (0)