Skip to content

Commit 296e400

Browse files
Merge pull request algorithm001#683 from v1xingyue/master
007-Week4 作业
2 parents 47a4776 + 83eeee0 commit 296e400

7 files changed

Lines changed: 238 additions & 1 deletion

File tree

Week_04/id_7/LeetCode_146_7.go

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package geekcode
2+
3+
import (
4+
"container/list"
5+
)
6+
7+
type CacheItem struct {
8+
key int
9+
value interface{}
10+
}
11+
12+
type LRUCache struct {
13+
dlist *list.List
14+
mapItems map[int]*list.Element
15+
capacity int
16+
}
17+
18+
func Constructor(max int) LRUCache {
19+
l := LRUCache{}
20+
l.capacity = max
21+
l.Init()
22+
return l
23+
}
24+
25+
func (self *LRUCache) Init() {
26+
self.dlist = new(list.List)
27+
self.mapItems = make(map[int]*list.Element)
28+
}
29+
30+
// 插入新数据的时候,对列表的操作需要注意:
31+
// 如果插入后,列表将满,那么先删除元素,再做插入
32+
func (self *LRUCache) Put(key int, value interface{}) {
33+
if v, ok := self.mapItems[key]; ok && v != nil {
34+
v.Value.(*CacheItem).value = value
35+
self.dlist.MoveToFront(v)
36+
} else {
37+
i := &CacheItem{key: key, value: value}
38+
self.mapItems[key] = self.dlist.PushFront(i)
39+
self.Reduce()
40+
}
41+
}
42+
43+
func (self *LRUCache) Len() int {
44+
return self.dlist.Len()
45+
}
46+
47+
//获取数据以后,
48+
//数据移动顺序和插入,更新数据时候移动的方向是一致的
49+
func (self *LRUCache) Get(key int) interface{} {
50+
n := self.mapItems[key]
51+
if n != nil {
52+
self.dlist.MoveToFront(n)
53+
return n.Value.(*CacheItem).value
54+
} else {
55+
return -1
56+
}
57+
}
58+
59+
func (self *LRUCache) Reduce() {
60+
if self.Len() > self.capacity {
61+
n := self.dlist.Back()
62+
if n != nil {
63+
mapKey := n.Value.(*CacheItem).key
64+
delete(self.mapItems, mapKey)
65+
self.dlist.Remove(n)
66+
}
67+
}
68+
}

Week_04/id_7/LeetCode_720_7.go

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
package geekcode
2+
3+
// 存储节点数据
4+
// 省去一个isend , 用word == "" 来代替
5+
// 添加parent 指针,用来做返回判断
6+
type tries_node struct {
7+
children map[byte]*tries_node
8+
word string // 标记当前节点表示单词
9+
parent *tries_node // 标记父节点,省去遍历的麻烦
10+
}
11+
12+
//往tries 树中添加一个单词
13+
func (self *tries_node) addString(word string) *tries_node {
14+
last := self
15+
for _, b := range word {
16+
_b := byte(b)
17+
if last.children == nil {
18+
last.children = make(map[byte]*tries_node)
19+
}
20+
21+
var n *tries_node
22+
var ok bool
23+
24+
if n, ok = last.children[_b]; ok == false {
25+
n = &tries_node{children: nil, word: "", parent: last}
26+
last.children[_b] = n
27+
}
28+
last = n
29+
}
30+
last.word = word
31+
return last
32+
}
33+
34+
// 当前的单词,是否可以由其他的单词添加字符组成,
35+
// 从跟节点到当前节点走过的所有节点,必须world 不为空
36+
func (self *tries_node) isPathAllWord() (bool, int) {
37+
len := 0
38+
p := self
39+
for p.parent != nil {
40+
if p.word == "" {
41+
return false, 0
42+
}
43+
len += 1
44+
p = p.parent
45+
}
46+
return true, len
47+
}
48+
49+
// 判断 a,b 的字典顺序,是否是 a > b
50+
// 前提 a,b 的长度相等
51+
func isab(a, b string) bool {
52+
l := len(a)
53+
for i := 0; i < l; i++ {
54+
if byte(a[i]) > byte(b[i]) {
55+
return true
56+
}
57+
if byte(a[i]) < byte(b[i]) {
58+
return false
59+
}
60+
}
61+
return false
62+
}
63+
64+
//使用字典创建tries 树,获取深度最长的叶子节点
65+
func longestWord(words []string) string {
66+
var word_nodes = make([]*tries_node, 0)
67+
root_tree := new(tries_node)
68+
root_tree.word = "-"
69+
// 每个单词添加到tries 树中
70+
for _, v := range words {
71+
l := root_tree.addString(v)
72+
word_nodes = append(word_nodes, l)
73+
}
74+
75+
maxl := 0
76+
var maxnode *tries_node
77+
for _, w := range word_nodes {
78+
//fmt.Println(w.word)
79+
ok, l := w.isPathAllWord()
80+
if ok {
81+
if maxl < l {
82+
maxl = l
83+
maxnode = w
84+
continue
85+
}
86+
if maxl == l {
87+
//fmt.Println(maxnode.word, w.word)
88+
if isab(maxnode.word, w.word) == true {
89+
maxnode = w
90+
}
91+
}
92+
}
93+
}
94+
95+
return maxnode.word
96+
}

Week_04/id_7/LeetCode_784_7.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package geekcode
2+
3+
// 784. 字母大小写全排列
4+
func letterCasePermutation(S string) []string {
5+
l := len(S)
6+
prefix := []string{""}
7+
for i := 0; i < l; i++ {
8+
n := make([]string, 0)
9+
c := S[i]
10+
for _, s := range prefix {
11+
n = append(n, s+string(c))
12+
// A ~ Z
13+
if byte(c) >= 65 && byte(c) <= 90 {
14+
n = append(n, s+string(c+32))
15+
}
16+
// a ~ z
17+
if byte(c) >= 97 && byte(c) <= 122 {
18+
n = append(n, s+string(c-32))
19+
}
20+
}
21+
22+
prefix = n
23+
}
24+
return prefix
25+
}

Week_04/id_7/NOTE.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,10 @@
1-
# 学习笔记
1+
# 学习笔记
2+
3+
* [LRU缓存机制](https://leetcode-cn.com/problems/lru-cache/)
4+
[LeetCode_146_7.go](LeetCode_146_7.go)
5+
6+
* [词典中最长的单词](https://leetcode-cn.com/problems/longest-word-in-dictionary/)
7+
[LeetCode_720_7.go](LeetCode_720_7.go)
8+
9+
* [字母大小写全排列](https://leetcode-cn.com/problems/letter-case-permutation/submissions/)
10+
[LeetCode_784_7.go](LeetCode_784_7.go)

Week_04/id_7/geek_test.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package geekcode
2+
3+
import (
4+
"testing"
5+
)
6+
7+
func TestLongestWord(t *testing.T) {
8+
9+
words := []string{"t", "ti", "tig", "tige", "tiger", "e", "en", "eng", "engl", "engli", "englis", "english", "h", "hi", "his", "hist", "histo", "histor", "history"}
10+
t.Log(longestWord(words))
11+
}

Week_04/id_7/lru_test.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package geekcode
2+
3+
import (
4+
"testing"
5+
)
6+
7+
func TestLRU(t *testing.T) {
8+
cache := Constructor(2)
9+
cache.Put(1, 1)
10+
cache.Put(2, 2)
11+
t.Log(cache.Get(1), " should be 1 ")
12+
cache.Put(3, 3)
13+
t.Log(cache.Get(2), " should be -1 ")
14+
cache.Put(4, 4)
15+
t.Log(cache.Get(1), " should be -1 ")
16+
t.Log(cache.Get(3), " should be 3")
17+
t.Log(cache.Get(4), " should be 4")
18+
}

Week_04/id_7/readme.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# 学习笔记
2+
3+
* [LRU缓存机制](https://leetcode-cn.com/problems/lru-cache/)
4+
[LeetCode_146_7.go](LeetCode_146_7.go)
5+
6+
* [词典中最长的单词](https://leetcode-cn.com/problems/longest-word-in-dictionary/)
7+
[LeetCode_720_7.go](LeetCode_720_7.go)
8+
9+
* [字母大小写全排列](https://leetcode-cn.com/problems/letter-case-permutation/submissions/)
10+
[LeetCode_784_7.go](LeetCode_784_7.go)

0 commit comments

Comments
 (0)