This commit is contained in:
2026-06-21 20:46:23 +10:00
parent c4cfce490e
commit f873a89c82
6 changed files with 227 additions and 0 deletions
+90
View File
@@ -0,0 +1,90 @@
package top100liked
import (
"fmt"
"testing"
)
// https://leetcode.cn/problems/lru-cache/?envType=study-plan-v2&envId=top-100-liked
type LN struct {
Key int
Val int
Prev *LN
Next *LN
}
type LRUCache struct {
length int
capacity int
dummyHead *LN
dummyTail *LN
kv map[int]*LN
}
func Constructor(capacity int) LRUCache {
head := &LN{}
tail := &LN{}
head.Next = tail
tail.Prev = head
return LRUCache{
length: 0,
capacity: capacity,
dummyHead: head,
dummyTail: tail,
kv: make(map[int]*LN, capacity),
}
}
func (this *LRUCache) removeNode(node *LN) {
node.Prev.Next = node.Next
node.Next.Prev = node.Prev
}
func (this *LRUCache) addToHead(node *LN) {
node.Next = this.dummyHead.Next
node.Prev = this.dummyHead
this.dummyHead.Next.Prev = node
this.dummyHead.Next = node
}
func (this *LRUCache) Get(key int) int {
if node, ok := this.kv[key]; ok {
this.removeNode(node)
this.addToHead(node)
return node.Val
}
return -1
}
func (this *LRUCache) Put(key int, value int) {
if node, ok := this.kv[key]; ok {
node.Val = value
this.removeNode(node)
this.addToHead(node)
return
}
if this.length >= this.capacity {
lru := this.dummyTail.Prev
this.removeNode(lru)
delete(this.kv, lru.Key)
} else {
this.length++
}
newNode := &LN{Key: key, Val: value}
this.addToHead(newNode)
this.kv[key] = newNode
}
func Test1(t *testing.T) {
lRUCache := Constructor(2)
lRUCache.Put(1, 1)
lRUCache.Put(2, 2)
fmt.Println(lRUCache.Get(1))
lRUCache.Put(3, 3)
fmt.Println(lRUCache.Get(2))
lRUCache.Put(4, 4)
fmt.Println(lRUCache.Get(1))
fmt.Println(lRUCache.Get(3))
fmt.Println(lRUCache.Get(4))
}
+32
View File
@@ -0,0 +1,32 @@
package top100liked
import (
"testing"
)
// https://leetcode.cn/problems/binary-tree-inorder-traversal/?envType=study-plan-v2&envId=top-100-liked
type TreeNode struct {
Val int
Left *TreeNode
Right *TreeNode
}
func inorderTraversal(root *TreeNode) []int {
if root == nil {
return []int{}
}
res := []int{}
if root.Left != nil {
res = append(res, inorderTraversal(root.Left)...)
}
res = append(res, root.Val)
if root.Right != nil {
res = append(res, inorderTraversal(root.Right)...)
}
return res
}
func Test1(t *testing.T) {
}
+34
View File
@@ -0,0 +1,34 @@
package top100liked
import (
"testing"
)
// https://leetcode.cn/problems/maximum-depth-of-binary-tree/description/?envType=study-plan-v2&envId=top-100-liked
type TreeNode struct {
Val int
Left *TreeNode
Right *TreeNode
}
func maxDepth(root *TreeNode) int {
if root == nil {
return 0
}
if root.Left == nil && root.Right == nil {
return 1
}
md := 0
if root.Left != nil {
md = maxDepth(root.Left) + 1
}
if root.Right != nil {
md = max(maxDepth(root.Right)+1, md)
}
return md
}
func Test1(t *testing.T) {
}
+27
View File
@@ -0,0 +1,27 @@
package top100liked
import (
"testing"
)
// https://leetcode.cn/problems/invert-binary-tree/description/?envType=study-plan-v2&envId=top-100-liked
type TreeNode struct {
Val int
Left *TreeNode
Right *TreeNode
}
func invertTree(root *TreeNode) *TreeNode {
if root == nil {
return nil
}
root.Left, root.Right = root.Right, root.Left
invertTree(root.Left)
invertTree(root.Right)
return root
}
func Test1(t *testing.T) {
}
+31
View File
@@ -0,0 +1,31 @@
package top100liked
import (
"testing"
)
// https://leetcode.cn/problems/symmetric-tree/description/?envType=study-plan-v2&envId=top-100-liked
type TreeNode struct {
Val int
Left *TreeNode
Right *TreeNode
}
func isSymmetric(root *TreeNode) bool {
return check(root.Left, root.Right)
}
func check(p, q *TreeNode) bool {
if p == nil && q == nil {
return true
}
if p == nil || q == nil {
return false
}
return p.Val == q.Val && check(p.Left, q.Right) && check(p.Right, q.Left)
}
func Test1(t *testing.T) {
}
+13
View File
@@ -0,0 +1,13 @@
package top100liked
// https://leetcode.cn/problems/diameter-of-binary-tree/description/?envType=study-plan-v2&envId=top-100-liked
type TreeNode struct {
Val int
Left *TreeNode
Right *TreeNode
}
func diameterOfBinaryTree(root *TreeNode) int {
}