func canConstruct(ransomNote string, magazine string) bool {
if len(ransomNote) == 0 && len(magazine) == 0 || len(ransomNote) == 0 {
return true
}
if len(magazine) == 0 {
return false
}
dict1 := make(map[rune]int, 0)
dict2 := make(map[rune]int, 0)
for _, v := range ransomNote {
dict1[v]++
}
for _, v := range magazine {
dict2[v]++
}
for k1, v1 := range dict1 {// 这个是子集,所以条件成立的话。父级在k1元素中是有值的。且k2的元素比k1的大
if v2, ok := dict2[k1]; !ok || v2 < v1 {
return false
}
}
return true
}
byte和rune的区别
byte和rune
源码type byte = uint8
// rune is an alias for int32 and is equivalent to int32 in all ways. It is
// used, by convention, to distinguish character values from integer values.
type rune = int32
byte
first := "fisrt"
fmt.Println([]rune(first))
fmt.Println([]byte(first))
[102 105 115 114 116] // 输出结果 [] rune
[102 105 115 114 116] // 输出结果 [] byte
rune
first := "社区"
fmt.Println([]rune(first))
fmt.Println([]byte(first))
[31038 21306] // 输出结果 [] rune
[231 164 190 229 140 186]// 输出结果 [] byte
截取中文字符串
package main
import "fmt"
func main() {
s := "中国很大"
res := []rune(s)
fmt.Println(string(res[:2]))
}
// 中国