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 main
import ( "fmt" "sort" )
type Person struct { Name string Age int }
type PersonSlice [] Person
func (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
| type unionSet struct { fa [100004]int }
func (u *unionSet) find(x int) int { r := x for r != u.fa[r] { r = u.fa[r] }
i, j := x, r for i!=j { j = u.fa[i] u.fa[i]=r i=j }
return r }
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 15 16 17 18
| type box struct { num int size int }
type boxSlice []box func(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 13 14 15 16 17 18 19 20 21 22 23 24
| lis := make([]int, 0) for _, v := range arr { if len(lis) == 0 { lis = append(lis, v) } else { if lis[len(lis)-1] != MOD { lis = append(lis, MOD) } pos := func() int { l, r, mid, ans := 0, len(lis)-1, -1, -1 for l<=r { mid = (l+r) >> 1 if lis[mid] >= v { r, ans = mid-1, mid } else { l = mid+1 } } return ans }() lis[pos] = v } }
|
BIT(树状数组)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| var sum [100009]int
func lowbit(x int) int { return x&-x }
func up(sum [100009]int, i, v int) { for ;i<=100000; i+=lowbit(i) { sum[i]+=v } }
func get(sum [100009]int, i int) int { ans := 0 for ; i>0; i-=lowbit(i) { ans += sum[i] } return ans }
|