中文:在线入门指引http://go-tour-zh.appspot.com/basics/1 中文:文档首页https://go-zh.org/doc/ 中文首页https://go-zh.org/ 英文首页https://golang.org/
Go 中文主页
先把这个教程看完 - Go 指南
然后就去写代码
完毕
go 语言常用的一些代码 (刷题 自定义排序 [【Go语言】基本类型排序和 slice 排序](https://itimetraveler.github.io/2016/09/07/【Go语言】基本类型排序和 slice 排序/)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 package mainimport ( "fmt" "sort" ) type Person struct { Name string Age int } type PersonSlice [] Personfunc (a PersonSlice) Len() int { return len (a) } func (a PersonSlice) Swap(i, j int ){ a[i], a[j] = a[j], a[i] } func (a PersonSlice) Less(i, j int ) bool { return a[j].Age < a[i].Age } func main () { people := [] Person{ {"zhang san" , 12 }, {"li si" , 30 }, {"wang wu" , 52 }, {"zhao liu" , 26 }, } fmt.Println(people) sort.Sort(PersonSlice(people)) fmt.Println(people) sort.Sort(sort.Reverse(PersonSlice(people))) fmt.Println(people) }
比较大小 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 func max (a, b int ) int { if a > b { return a } else { return b } } func min (a, b int ) int { if a < b { return a } else { return b } }
模拟C++的Vector 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 ans := make ([]int , 0 ) ans = append (ans, 23 ) func groupThePeople (groupSizes []int ) [][]int { ans := make ([][]int , 0 ) cnt := make ([][]int , 500 ) for i, v := range groupSizes { cnt[v] = append (cnt[v], i) if len (cnt[v]) == v { ans = append (ans, cnt[v]) cnt[v] = make ([]int , 0 ) } } return ans }
刷题板子 LCS 1 2 3 4 5 6 7 8 9 10 11 12 int dp[1005 ][1005 ];int lcs(string word1, string word2) { int ls1 = word1.size(), ls2 = word2.size(); for (int i = 1 ; i<=ls1; i++) for (int j = 1 ; j<=ls2; j++) { if (word1[i-1 ] == word2[j-1 ]) { dp[i][j] = dp[i-1 ][j-1 ] + 1 ; } else { dp[i][j] = max(dp[i-1 ][j], dp[i][j-1 ]); } } return dp[ls1][ls2]; }
二进制枚举 1 2 3 for (int j = 1 ; j < (1 << n); j += 1 ) for (int x = j; x; x = (x - 1 ) & j)
并查集 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 type unionSet struct { fa []int } func NewUnionSet (n int ) unionSet { fa := make ([]int , n) for i := range fa { fa[i] = i } return unionSet{fa} } func (u *unionSet) Find(x int ) int { if x != u.fa[x] { u.fa[x] = u.Find(u.fa[x]) } return u.fa[x] } func (u *unionSet) Join(x, y int ) { fx, fy := u.Find(x), u.Find(y) if fx != fy { u.fa[fy]=fx } }
自定义结构体+排序 1 2 3 4 5 6 7 8 9 10 11 12 13 14 type box struct { num int size int } type boxSlice []boxfunc (t boxSlice) Len() int {return len (t)}func (t boxSlice) Less(i, j int ) bool {return t[i].size > t[j].size}func (t boxSlice) Swap(i, j int ) {t[i], t[j] = t[j], t[i]} a := []box sort.Sort(boxSlice(a))
LIS 1 2 3 4 5 6 7 8 9 10 11 12 lis := []int {} for _, v := range arr { p := sort.SearchInts(lis, v+1 ) if p < len (lis) { lis[p] = v } else { lis = append (lis, v) } }
BIT(树状数组) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 type BIT struct { sum [100009 ]int } func lowbit (x int ) int { return x&-x } func (bit *BIT) up(i, v int ) { for ;i<=100000 ; i+=lowbit(i) { bit.sum[i]+=v } } func (bit *BIT) get(i int ) int { ans := 0 for ; i>0 ; i-=lowbit(i) { ans += bit.sum[i] } return ans }
离散化 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 func lisanhua (nums []int ) []int { temp := make ([]int , len (nums)) copy (temp, nums) sort.Ints(temp) lt := 0 for i := range temp { if i > 0 && temp[i] == temp[i-1 ] { continue } temp[lt] = temp[i] lt++ } for i := range nums { nums[i] = sort.SearchInts(temp, nums[i]) + 1 } return nums }