• labuladong算法小抄-数据结构设计-leetcode146、leetcode341、leetcode380、leetcode460


    例题

    146. LRU 缓存机制

    1. package main
    2. func main() {
    3. /**
    4. * Your LRUCache object will be instantiated and called as such:
    5. * obj := Constructor(capacity);
    6. * param_1 := obj.Get(key);
    7. * obj.Put(key,value);
    8. */
    9. obj := Constructor(2)
    10. obj.Put(1,1)
    11. obj.Put(2,2)
    12. println(obj.Get(1))
    13. obj.Put(3,3)
    14. println(obj.Get(2))
    15. obj.Put(4,4)
    16. println(obj.Get(1))
    17. println(obj.Get(3))
    18. println(obj.Get(4))
    19. }
    20. //["LRUCache","put","put","get","put","get","put","get","get","get"]
    21. //[[2],[1,1],[2,2],[1],[3,3],[2],[4,4],[1],[3],[4]]
    22. type LRUCache struct {
    23. size int
    24. capacity int
    25. mymap map[int]*Node
    26. head,tail *Node
    27. }
    28. type Node struct{
    29. key int
    30. val int
    31. pre , next *Node
    32. }
    33. func Constructor(capacity int) LRUCache {
    34. lru := LRUCache{
    35. size: 0,
    36. capacity: capacity,
    37. mymap: make(map[int]*Node),
    38. head: &Node{
    39. 0,
    40. 0,
    41. nil,
    42. nil,
    43. },
    44. tail: &Node{
    45. 0,
    46. 0,
    47. nil,
    48. nil,
    49. },
    50. }
    51. lru.head.next = lru.tail
    52. lru.tail.pre = lru.head
    53. return lru
    54. }
    55. func (this *LRUCache) RemoveNode(node *Node) {
    56. node.pre.next = node.next
    57. node.next.pre = node.pre
    58. }
    59. func (this *LRUCache)AddHead(node *Node) {
    60. this.head.next.pre = node
    61. node.next = this.head.next
    62. this.head.next = node
    63. node.pre = this.head
    64. }
    65. func (this *LRUCache)RmoveTail() *Node{
    66. res := this.tail.pre
    67. this.tail.pre.pre.next = this.tail
    68. this.tail.pre = this.tail.pre.pre
    69. return res
    70. }
    71. func (this *LRUCache) Get(key int) int {
    72. val ,e := this.mymap[key]
    73. if !e{
    74. return -1
    75. }
    76. this.RemoveNode(val)
    77. this.AddHead(val)
    78. return val.val
    79. }
    80. func (this *LRUCache) Put(key int, value int) {
    81. val , e := this.mymap[key]
    82. // 不存在
    83. if !e{
    84. tep := &Node{
    85. key: key,
    86. val: value,
    87. pre: nil,
    88. next: nil,
    89. }
    90. this.AddHead(tep)
    91. this.mymap[key] = tep
    92. this.size++
    93. if this.size > this.capacity {
    94. this.size --
    95. ddkey := this.RmoveTail()
    96. delete(this.mymap,ddkey.key)
    97. }
    98. }else{
    99. val.val = value
    100. this.RemoveNode(val)
    101. this.AddHead(val)
    102. }
    103. }

    思想:哈希+双向链表

    341. 扁平化嵌套列表迭代器

    1. /**
    2. * // This is the interface that allows for creating nested lists.
    3. * // You should not implement it, or speculate about its implementation
    4. * type NestedInteger struct {
    5. * }
    6. *
    7. * // Return true if this NestedInteger holds a single integer, rather than a nested list.
    8. * func (this NestedInteger) IsInteger() bool {}
    9. *
    10. * // Return the single integer that this NestedInteger holds, if it holds a single integer
    11. * // The result is undefined if this NestedInteger holds a nested list
    12. * // So before calling this method, you should have a check
    13. * func (this NestedInteger) GetInteger() int {}
    14. *
    15. * // Set this NestedInteger to hold a single integer.
    16. * func (n *NestedInteger) SetInteger(value int) {}
    17. *
    18. * // Set this NestedInteger to hold a nested list and adds a nested integer to it.
    19. * func (this *NestedInteger) Add(elem NestedInteger) {}
    20. *
    21. * // Return the nested list that this NestedInteger holds, if it holds a nested list
    22. * // The list length is zero if this NestedInteger holds a single integer
    23. * // You can access NestedInteger's List element directly if you want to modify it
    24. * func (this NestedInteger) GetList() []*NestedInteger {}
    25. */
    26. type NestedIterator struct {
    27. nums []int
    28. index int
    29. max int
    30. }
    31. func Constructor(nestedList []*NestedInteger) *NestedIterator {
    32. res := NestedIterator{
    33. make([]int,0),
    34. 0,
    35. 0,
    36. }
    37. clen := len(nestedList)
    38. for i:=0;i
    39. if nestedList[i].IsInteger(){
    40. res.nums = append(res.nums,nestedList[i].GetInteger())
    41. }else{
    42. res.addToSlice(nestedList[i])
    43. }
    44. }
    45. res.max = len(res.nums)
    46. return &res
    47. }
    48. func (this *NestedIterator)addToSlice(n *NestedInteger){
    49. if n.IsInteger(){
    50. this.nums = append(this.nums,n.GetInteger())
    51. return
    52. }else{
    53. tep := n.GetList()
    54. clen := len(tep)
    55. for i:=0;i
    56. this.addToSlice(tep[i])
    57. }
    58. }
    59. }
    60. func (this *NestedIterator) Next() int {
    61. t := this.index
    62. this.index++
    63. return this.nums[t]
    64. }
    65. func (this *NestedIterator) HasNext() bool {
    66. if this.index < this.max{
    67. return true
    68. }else {
    69. return false
    70. }
    71. }

    380. O(1) 时间插⼊、删除和获取随机元素

    1. type RandomizedSet struct {
    2. mymap map[int]int
    3. nums []int
    4. len int
    5. }
    6. func Constructor() RandomizedSet {
    7. res := RandomizedSet{
    8. make(map[int]int),
    9. make([]int,0),
    10. 0,
    11. }
    12. return res
    13. }
    14. func (this *RandomizedSet) Insert(val int) bool {
    15. _ ,e := this.mymap[val]
    16. if e{
    17. return false
    18. }
    19. this.mymap[val] = this.len
    20. this.len ++
    21. this.nums = append(this.nums,val)
    22. return true
    23. }
    24. func (this *RandomizedSet) Remove(val int) bool {
    25. index ,e := this.mymap[val]
    26. if !e{
    27. return false
    28. }
    29. lenKey := this.nums[this.len-1]
    30. this.mymap[lenKey] = index
    31. this.nums[index] = lenKey
    32. this.nums = this.nums[:this.len-1]
    33. this.len --
    34. delete(this.mymap,val)
    35. return true
    36. }
    37. func (this *RandomizedSet) GetRandom() int {
    38. return this.nums[rand.Intn(this.len)]
    39. }

    思想:哈希+slice,在进行移除slice的时候,可以考虑先进行置换在进行移除,这样就不会影响哈希对应的下标,一般是将移除的index和末尾进行交换之后在进行移除。

    460. LFU 缓存

    895. 最⼤频率栈

    1. type FreqStack struct {
    2. keyTime map[int]int
    3. timeSlice map[int][]int
    4. MAX int
    5. }
    6. func Constructor() FreqStack {
    7. res := FreqStack{
    8. make(map[int]int),
    9. make(map[int][]int),
    10. 0,
    11. }
    12. return res
    13. }
    14. func (this *FreqStack) Push(val int) {
    15. // 存在
    16. if time, e := this.keyTime[val]; !e {
    17. this.keyTime[val] = 1
    18. if slice , e2 := this.timeSlice[1];!e2{
    19. cslice := []int{val}
    20. this.timeSlice[1] = cslice
    21. }else{
    22. slice = append(slice , val)
    23. this.timeSlice[1] = slice
    24. }
    25. if this.MAX < 1{
    26. this.MAX = 1
    27. }
    28. } else {// 不存在
    29. this.keyTime[val] = time + 1
    30. time ++
    31. if slice , e2 := this.timeSlice[time];!e2{
    32. cslice := []int{val}
    33. this.timeSlice[time] = cslice
    34. }else{
    35. slice = append(slice , val)
    36. this.timeSlice[time] = slice
    37. }
    38. if this.MAX < time{
    39. this.MAX = time
    40. }
    41. }
    42. }
    43. func (this *FreqStack) Pop() int {
    44. for this.MAX !=0 {
    45. if len(this.timeSlice[this.MAX]) > 0{
    46. tepSlice := this.timeSlice[this.MAX]
    47. res := tepSlice[len(tepSlice) -1 ]
    48. this.timeSlice[this.MAX] = tepSlice[:len(tepSlice)-1]
    49. ctime := this.keyTime[res]
    50. ctime --
    51. if ctime <= 0{
    52. delete(this.keyTime,res)
    53. }else{
    54. this.keyTime[res] =ctime
    55. }
    56. return res
    57. }else{
    58. this.MAX --
    59. }
    60. }
    61. return -1
    62. }
    63. /**
    64. * Your FreqStack object will be instantiated and called as such:
    65. * obj := Constructor();
    66. * obj.Push(val);
    67. * param_2 := obj.Pop();
    68. */

  • 相关阅读:
    【Elasticsearch】es基础入门-03.RestClient操作文档
    什么是 vuejs 加载器?
    大气化学在线耦合模式WRF/Chem
    【Linux常用命令12】搜索命令及特殊字符的使用
    14.ElasticSearch系列之分布式特性及分布式搜索机制(三)
    HTML5期末大作业:旅游网页设计与实现——旅游风景区网站HTML+CSS+JavaScript 景点静态网页设计 学生DW静态网页设计
    dubbo启动之注册中心(Registry)
    “Redis与Spring整合及缓存优化“
    Text2SQL中不同数据库SQL之间转换的实战代码
    leetcode 37. 解数独 (困难)
  • 原文地址:https://blog.csdn.net/qq_41593124/article/details/126066295