概述

  • map key 无序 是hashmap
  • make(map[string]int) 创建map
  • key 不存在,活动value类型的初始值
  • 获取元素 m[key]
  1. v, ok := m[key] //判断是否存在key
  2. for k, v := range m //遍历,但不保证顺序,手动对key排序。把key加到slice里面。
  • map 的key

必须可以比较相等。除了slice,map,function的内建类型都可以作为key。
Struct 类型不包含上述字段,也可作为key。编译是检查

  • rune

utf8.RuneCountInString(s)) 获取字符数量
len(s)字节长度
[]byte(s) 获得字节

代码

  1. package main
  2. import "fmt"
  3. func main() {
  4. m := map[string]string{
  5. "name": "ccmouse",
  6. "course": "golang",
  7. "site": "imooc",
  8. "quality": "notbad",
  9. }
  10. m2 := make(map[string]int) // m2 == empty map
  11. var m3 map[string]int // m3 == nil
  12. fmt.Println("m, m2, m3:")
  13. fmt.Println(m, m2, m3)
  14. fmt.Println("Traversing map m")
  15. for k, v := range m {
  16. fmt.Println(k, v)
  17. }
  18. fmt.Println("Getting values")
  19. courseName := m["course"]
  20. fmt.Println(`m["course"] =`, courseName)
  21. //判断key 是否存在
  22. if causeName, ok := m["cause"]; ok {
  23. fmt.Println(causeName)
  24. } else {
  25. fmt.Println("key 'cause' does not exist")
  26. }
  27. fmt.Println("Deleting values")
  28. name, ok := m["name"]
  29. fmt.Printf("m[%q] before delete: %q, %v\n",
  30. "name", name, ok)
  31. //删除
  32. delete(m, "name")
  33. name, ok = m["name"]
  34. fmt.Printf("m[%q] after delete: %q, %v\n",
  35. "name", name, ok)
  36. }

longest-substring-without-repeating-characters

Given a string, find the length of the longest substring without repeating characters. For example, the longest substring without repeating letters for “abcabcbb” is “abc”, which the length is 3. For “bbbbb” the longest substring is “b”, with the length of 1.

  1. package main
  2. import (
  3. "fmt"
  4. )
  5. func lengthOfNonRepeatingSubStr(s string) int {
  6. lastOccurred := make(map[rune]int)
  7. start := 0
  8. maxLength := 0
  9. for i, ch := range []rune(s) {
  10. if lastI, ok := lastOccurred[ch]; ok && lastI >= start {
  11. start = lastI + 1
  12. }
  13. if i-start+1 > maxLength {
  14. maxLength = i - start + 1
  15. }
  16. lastOccurred[ch] = i
  17. }
  18. return maxLength
  19. }
  20. func main() {
  21. fmt.Println(
  22. lengthOfNonRepeatingSubStr("abcabcbb"))
  23. fmt.Println(
  24. lengthOfNonRepeatingSubStr("bbbbb"))
  25. fmt.Println(
  26. lengthOfNonRepeatingSubStr("pwwkew"))
  27. fmt.Println(
  28. lengthOfNonRepeatingSubStr(""))
  29. fmt.Println(
  30. lengthOfNonRepeatingSubStr("b"))
  31. fmt.Println(
  32. lengthOfNonRepeatingSubStr("abcdef"))
  33. fmt.Println(
  34. lengthOfNonRepeatingSubStr("这里是mmm"))
  35. fmt.Println(
  36. lengthOfNonRepeatingSubStr("一二三二一"))
  37. fmt.Println(
  38. lengthOfNonRepeatingSubStr(
  39. "asfasdjljsd;fjlasdj"))
  40. }

rune

  1. package main
  2. import (
  3. "fmt"
  4. "unicode/utf8"
  5. )
  6. func main() {
  7. s := "Yes我爱kk!" // UTF-8
  8. fmt.Println(s)
  9. for _, b := range []byte(s) {
  10. fmt.Printf("%X ", b)
  11. }
  12. fmt.Println()
  13. for i, ch := range s { // ch is a rune
  14. fmt.Printf("(%d %X) ", i, ch)
  15. }
  16. fmt.Println()
  17. fmt.Println("Rune count:",
  18. utf8.RuneCountInString(s))
  19. bytes := []byte(s)
  20. for len(bytes) > 0 {
  21. ch, size := utf8.DecodeRune(bytes)
  22. bytes = bytes[size:]
  23. fmt.Printf("%c ", ch)
  24. }
  25. fmt.Println()
  26. for i, ch := range []rune(s) {
  27. fmt.Printf("(%d %c) ", i, ch)
  28. }
  29. fmt.Println()
  30. }

image.jpeg