中文亚洲精品无码_熟女乱子伦免费_人人超碰人人爱国产_亚洲熟妇女综合网

當(dāng)前位置: 首頁 > news >正文

做類似美團(tuán)的網(wǎng)站得多少錢長尾關(guān)鍵詞在線查詢

做類似美團(tuán)的網(wǎng)站得多少錢,長尾關(guān)鍵詞在線查詢,高端網(wǎng)站定制費用是多少,平面設(shè)計培訓(xùn)課程學(xué)校文章目錄 1. container中定義的heap2. heap的使用示例3. 刷lc應(yīng)用堆的示例 更多內(nèi)容以及其他Go常用數(shù)據(jù)結(jié)構(gòu)的實現(xiàn)在這里,感謝Star:https://github.com/acezsq/Data_Structure_Golang 1. container中定義的heap 在golang中的"container/heap"…

文章目錄

      • 1. container中定義的heap
      • 2. heap的使用示例
      • 3. 刷lc應(yīng)用堆的示例

更多內(nèi)容以及其他Go常用數(shù)據(jù)結(jié)構(gòu)的實現(xiàn)在這里,感謝Star:https://github.com/acezsq/Data_Structure_Golang

1. container中定義的heap

在golang中的"container/heap"源碼包中定義了堆的實現(xiàn),我們在使用時需要實現(xiàn)heap接口中定義的方法,以此實現(xiàn)一個堆。
container/heap.go中的heap接口的定義如下:

type Interface interface {sort.InterfacePush(x any) // add x as element Len()Pop() any   // remove and return element Len() - 1.
}

而sort包中的接口定義如下:

type Interface interface {// Len is the number of elements in the collection.Len() int// Less reports whether the element with index i// must sort before the element with index j.//// If both Less(i, j) and Less(j, i) are false,// then the elements at index i and j are considered equal.// Sort may place equal elements in any order in the final result,// while Stable preserves the original input order of equal elements.//// Less must describe a transitive ordering://  - if both Less(i, j) and Less(j, k) are true, then Less(i, k) must be true as well.//  - if both Less(i, j) and Less(j, k) are false, then Less(i, k) must be false as well.//// Note that floating-point comparison (the < operator on float32 or float64 values)// is not a transitive ordering when not-a-number (NaN) values are involved.// See Float64Slice.Less for a correct implementation for floating-point values.Less(i, j int) bool// Swap swaps the elements with indexes i and j.Swap(i, j int)
}

所以我們實現(xiàn)一個堆時需要實現(xiàn)這五個方法,然后相當(dāng)于實現(xiàn)了這個接口,然后就可以調(diào)用container/heap.go中定義的Init方法、Push方法、Pop方法進(jìn)行堆的基礎(chǔ)入堆、出堆操作。
在使用這三個方法時,需要注意按照源碼中定義的函數(shù)的入?yún)⒑头祷刂档念愋蛠硎褂谩?/p>

// Init establishes the heap invariants required by the other routines in this package.
// Init is idempotent with respect to the heap invariants
// and may be called whenever the heap invariants may have been invalidated.
// The complexity is O(n) where n = h.Len().
func Init(h Interface) {// heapifyn := h.Len()for i := n/2 - 1; i >= 0; i-- {down(h, i, n)}
}
// Push pushes the element x onto the heap.
// The complexity is O(log n) where n = h.Len().
func Push(h Interface, x any) {h.Push(x)up(h, h.Len()-1)
}
// Pop removes and returns the minimum element (according to Less) from the heap.
// The complexity is O(log n) where n = h.Len().
// Pop is equivalent to Remove(h, 0).
func Pop(h Interface) any {n := h.Len() - 1h.Swap(0, n)down(h, 0, n)return h.Pop()
}

2. heap的使用示例

在golang的源碼中也有堆的使用示例:
可以看到實現(xiàn)上我們用切片來作為heap的底層實現(xiàn)類型。
下面的代碼是定義一個小根堆的示例,如果我們想定義一個存int類型數(shù)據(jù)的大根堆,只需要把Less函數(shù)中的小于號換成大于號即可。

// Copyright 2012 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.// This example demonstrates an integer heap built using the heap interface.
package heap_testimport ("container/heap""fmt"
)// An IntHeap is a min-heap of ints.
type IntHeap []intfunc (h IntHeap) Len() int           { return len(h) }
func (h IntHeap) Less(i, j int) bool { return h[i] < h[j] }
func (h IntHeap) Swap(i, j int)      { h[i], h[j] = h[j], h[i] }func (h *IntHeap) Push(x any) {// Push and Pop use pointer receivers because they modify the slice's length,// not just its contents.*h = append(*h, x.(int))
}func (h *IntHeap) Pop() any {old := *hn := len(old)x := old[n-1]*h = old[0 : n-1]return x
}// This example inserts several ints into an IntHeap, checks the minimum,
// and removes them in order of priority.
func Example_intHeap() {h := &IntHeap{2, 1, 5}heap.Init(h)heap.Push(h, 3)fmt.Printf("minimum: %d\n", (*h)[0])for h.Len() > 0 {fmt.Printf("%d ", heap.Pop(h))}// Output:// minimum: 1// 1 2 3 5
}

3. 刷lc應(yīng)用堆的示例

我們看一下23. 合并 K 個升序鏈表
image.png
這個題需要定義一個小根堆來存鏈表節(jié)點指針。

/*** Definition for singly-linked list.* type ListNode struct {*     Val int*     Next *ListNode* }*/
func mergeKLists(lists []*ListNode) *ListNode {h := minHeap{}for _, head := range lists {if head != nil {h = append(h, head) }}     heap.Init(&h) dummyhead := &ListNode{}cur := dummyheadfor len(h)>0 {node := heap.Pop(&h).(*ListNode)if node.Next != nil {heap.Push(&h, node.Next)}cur.Next = nodecur = cur.Next}return dummyhead.Next
}type minHeap []*ListNode
func (h minHeap) Len() int {return len(h)}
func (h minHeap) Less(i,j int) bool {return h[i].Val<h[j].Val}
func (h minHeap) Swap(i,j int) { h[i], h[j] = h[j], h[i]}
func (h *minHeap) Push(x any) { *h = append(*h, x.(*ListNode))}
func (h *minHeap) Pop() any { old:=*h; n:=len(old); x:=old[n-1]; *h=old[:n-1]; return x}
http://www.risenshineclean.com/news/51265.html

相關(guān)文章:

  • 網(wǎng)站語音轉(zhuǎn)寫怎么做seo知識分享
  • 深圳注冊公司需要什么條件中國seo網(wǎng)站
  • html網(wǎng)頁制作背景圖片seo 優(yōu)化顧問
  • 大連seo推廣優(yōu)化安卓aso關(guān)鍵詞優(yōu)化
  • iis怎么做網(wǎng)站站長工具推薦網(wǎng)站
  • 代理域名網(wǎng)站的公司怎么知道網(wǎng)站有沒有被收錄
  • wordpress國內(nèi)外貿(mào)主題百度快速排名優(yōu)化技術(shù)
  • 做網(wǎng)站的時候?qū)挾榷荚趺磁W(wǎng)絡(luò)營銷策劃活動方案
  • 哪個網(wǎng)站賣自己做的手工藝品北京百度關(guān)鍵詞排名
  • 用什么做視頻網(wǎng)站百度網(wǎng)址收錄提交入口
  • 河田鎮(zhèn)建設(shè)局網(wǎng)站百度手機(jī)助手下載2022官方正版
  • 做征婚網(wǎng)站有哪些做網(wǎng)站的好處
  • 在公司做網(wǎng)站是什么職位seo整站優(yōu)化更能準(zhǔn)確獲得客戶
  • 做阿里巴巴網(wǎng)站可以貸款嗎百度網(wǎng)盤私人資源鏈接
  • 自建國際網(wǎng)站做電商資源搜索引擎
  • 做繁體書的網(wǎng)站安卓優(yōu)化大師破解版
  • 家庭網(wǎng)做網(wǎng)站營銷網(wǎng)站建設(shè)教學(xué)
  • 如何建設(shè)社交網(wǎng)站網(wǎng)絡(luò)項目平臺
  • 深圳營銷網(wǎng)站建設(shè)公司排名分銷平臺
  • 做pcb網(wǎng)站昆明網(wǎng)絡(luò)推廣
  • 網(wǎng)站聯(lián)系方式連接怎么做今日要聞10條
  • 網(wǎng)站建設(shè)服務(wù)公司案例app拉新怎么做
  • 鄭州網(wǎng)站建設(shè)公司價格360搜圖片識圖
  • 寧波專業(yè)制作網(wǎng)站上海關(guān)鍵詞排名推廣
  • 幾百元做網(wǎng)站百度推廣圖片
  • 晉江免費網(wǎng)站建設(shè)網(wǎng)絡(luò)推廣包括哪些
  • wordpress文章中文版深圳百度seo優(yōu)化
  • 廣州一網(wǎng)通注冊公司seo推廣方案怎么做
  • 適合前端做項目的網(wǎng)站dz論壇如何seo
  • 自己建的網(wǎng)站有亂碼成都網(wǎng)站設(shè)計