Skip to content

Commit 85a501b

Browse files
committed
update 146 Solution.go
1 parent fa1a01f commit 85a501b

File tree

7 files changed

+64
-2534
lines changed

7 files changed

+64
-2534
lines changed

leetcode/1-100/0072.Edit-Distance/Solution.go

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package Solution
22

3-
import "fmt"
4-
53
/*
64
设状态为dp[i][j],表示A[0,i]与B[0,j]的最小编辑距离,对应形式分别为str1c,str2d
75
如果c==d,f[i][j]=f[i-1][j-1]
@@ -41,13 +39,6 @@ func minDistance(word1 string, word2 string) int {
4139
return dp[m][n]
4240
}
4341

44-
func Print(arr [][]int) {
45-
for i := 0; i < len(arr); i++ {
46-
fmt.Println(arr[i])
47-
}
48-
fmt.Println()
49-
}
50-
5142
func min(x, y int) int {
5243
if x > y {
5344
return y
Lines changed: 64 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,82 @@
11
package Solution
22

3-
import (
4-
"container/list"
5-
"fmt"
6-
)
7-
83
type LRUCache struct {
9-
Capacity int
10-
Data *list.List
11-
//*list.List
4+
size int
5+
capacity int
6+
cache map[int]*DLinkedNode
7+
head, tail *DLinkedNode
8+
}
9+
10+
type DLinkedNode struct {
11+
key, value int
12+
prev, next *DLinkedNode
13+
}
14+
15+
func initDLinkedNode(key, value int) *DLinkedNode {
16+
return &DLinkedNode{
17+
key: key,
18+
value: value,
19+
}
1220
}
1321

1422
func Constructor(capacity int) LRUCache {
15-
lru := LRUCache{
16-
Capacity: capacity,
17-
Data: list.New(),
23+
l := LRUCache{
24+
cache: map[int]*DLinkedNode{},
25+
head: initDLinkedNode(0, 0),
26+
tail: initDLinkedNode(0, 0),
27+
capacity: capacity,
1828
}
19-
return lru
29+
l.head.next = l.tail
30+
l.tail.prev = l.head
31+
return l
2032
}
2133

2234
func (this *LRUCache) Get(key int) int {
23-
for e := this.Data.Front(); e != nil; e = e.Next() {
24-
25-
//fmt.Println(key, e.Value.(list.Element).Value.(map[int]int)[key])
26-
27-
if v, ok := e.Value.(list.Element).Value.(map[int]int)[key]; ok {
28-
this.Data.MoveBefore(e, this.Data.Front())
29-
return v
30-
}
35+
if _, ok := this.cache[key]; !ok {
36+
return -1
3137
}
32-
return -1
38+
node := this.cache[key]
39+
this.moveToHead(node)
40+
return node.value
3341
}
3442

3543
func (this *LRUCache) Put(key int, value int) {
36-
this.Data.PushFront(
37-
list.Element{
38-
Value: map[int]int{key: value},
39-
},
40-
)
41-
if this.Data.Len() > this.Capacity {
42-
this.Data.Remove(this.Data.Back())
44+
if _, ok := this.cache[key]; !ok {
45+
node := initDLinkedNode(key, value)
46+
this.cache[key] = node
47+
this.addToHead(node)
48+
this.size++
49+
if this.size > this.capacity {
50+
removed := this.removeTail()
51+
delete(this.cache, removed.key)
52+
this.size--
53+
}
54+
} else {
55+
node := this.cache[key]
56+
node.value = value
57+
this.moveToHead(node)
4358
}
4459
}
4560

46-
func (this *LRUCache) Print() {
47-
for e := this.Data.Front(); e != nil; e = e.Next() {
48-
fmt.Println(e.Value.(list.Element).Value.(map[int]int))
49-
}
61+
func (this *LRUCache) addToHead(node *DLinkedNode) {
62+
node.prev = this.head
63+
node.next = this.head.next
64+
this.head.next.prev = node
65+
this.head.next = node
66+
}
67+
68+
func (this *LRUCache) removeNode(node *DLinkedNode) {
69+
node.prev.next = node.next
70+
node.next.prev = node.prev
71+
}
72+
73+
func (this *LRUCache) moveToHead(node *DLinkedNode) {
74+
this.removeNode(node)
75+
this.addToHead(node)
76+
}
77+
78+
func (this *LRUCache) removeTail() *DLinkedNode {
79+
node := this.tail.prev
80+
this.removeNode(node)
81+
return node
5082
}

leetcode/101-200/0146.LRU-Cache/Solution_test.go

Lines changed: 0 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -2,35 +2,9 @@ package Solution
22

33
import (
44
"fmt"
5-
"reflect"
6-
"strconv"
75
"testing"
86
)
97

10-
func TestSolution(t *testing.T) {
11-
// 测试用例
12-
cases := []struct {
13-
name string
14-
inputs int
15-
expect bool
16-
}{
17-
{"TestCase", 1, true},
18-
{"TestCase", 1, true},
19-
{"TestCase", 1, false},
20-
}
21-
22-
// 开始测试
23-
for i, c := range cases {
24-
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
25-
got := Constructor(c.inputs)
26-
if !reflect.DeepEqual(got, c.expect) {
27-
//t.Fatalf("expected: %v, but got: %v, with inputs: %v",
28-
// c.expect, got, c.inputs)
29-
}
30-
})
31-
}
32-
}
33-
348
func TestConstructor(t *testing.T) {
359
lru := Constructor(2)
3610
fmt.Println(lru.Get(2))
@@ -40,19 +14,8 @@ func TestConstructor(t *testing.T) {
4014

4115
lru.Put(1, 5)
4216
lru.Put(1, 2)
43-
lru.Print()
4417

4518
fmt.Println(lru.Get(1))
4619
fmt.Println(lru.Get(2))
4720

4821
}
49-
50-
// 压力测试
51-
func BenchmarkSolution(b *testing.B) {
52-
53-
}
54-
55-
// 使用案列
56-
func ExampleSolution() {
57-
58-
}
Binary file not shown.
Binary file not shown.

0 commit comments

Comments
 (0)