• 周一. 4月 22nd, 2024

5G编程聚合网

5G时代下一个聚合的编程学习网

热门标签

Go map例题

admin

11月 28, 2021
 1 package main
 2 
 3 import "fmt"
 4 
 5 //map例题
 6 //寻找最长不含有重复字符的子串
 7 // abcabcbb  -> abc
 8 //pwwkew ->wke
 9 //对每一个字母x:
10 //   lastOccurred[x]不存在,或者< start -> 无需操作
11 //   lastOccurred[x] >= start -> 更新start
12 //   更新lastOccurred[x],更新maxLength
13 
14 func lengthOfNonRepeatinggSubStr( s string ) int {
15     lastOccurred := make( map[byte]int)
16     start := 0
17     maxLength := 0
18 
19     for i, ch := range  []byte(s) {
20 21         lastI ,ok := lastOccurred[ch]  //ok判断有没有key
22         if ok && lastI >= start{
23             start = lastI + 1
24         }
25         if i - start + 1 > maxLength{
26             maxLength = i - start + 1
27         }
28         lastOccurred[ch] = i
29     }
30     return  maxLength
31 }
32 func main() {
33     fmt.Println(lengthOfNonRepeatinggSubStr( "abcabccc"))  //3
34     fmt.Println(lengthOfNonRepeatinggSubStr( "abcabdefgh"))  //6
35 
36 }

 上面这个因为Unicode编码问题,它是不支持中文的,那go里面要怎么才能支持中文呢,这时候就要引出rune

rune相当于go的char ,这里把byte改成rune

package main

import "fmt"


func lengthOfNonRepeatinggSubStr( s string ) int {
    lastOccurred := make( map[rune]int)   //修改部分
    start := 0
    maxLength := 0

    for i, ch := range  []rune(s) {     //修改部分
        lastI ,ok := lastOccurred[ch]  //ok判断有没有key
        if ok && lastI >= start{
            start = lastI + 1
        }
        if i - start + 1 > maxLength{
            maxLength = i - start + 1
        }
        lastOccurred[ch] = i
    }
    return  maxLength
}
func main() {
    fmt.Println(lengthOfNonRepeatinggSubStr( "abcabccc"))  //3
    fmt.Println(lengthOfNonRepeatinggSubStr( "abcabdefgh"))  //6
    fmt.Println(lengthOfNonRepeatinggSubStr("我以为只要唱的用心良苦苦"))  //11

}

《Go map例题》有10个想法
  1. Sight Care is a daily supplement proven in clinical trials and conclusive science to improve vision by nourishing the body from within. The Sight Care formula claims to reverse issues in eyesight, and every ingredient is completely natural.

  2. Sumatra Slim Belly Tonic is a unique weight loss supplement that sets itself apart from others in the market. Unlike other supplements that rely on caffeine and stimulants to boost energy levels, Sumatra Slim Belly Tonic takes a different approach. It utilizes a blend of eight natural ingredients to address the root causes of weight gain. By targeting inflammation and improving sleep quality, Sumatra Slim Belly Tonic provides a holistic solution to weight loss. These natural ingredients not only support healthy inflammation but also promote better sleep, which are crucial factors in achieving weight loss goals. By addressing these underlying issues, Sumatra Slim Belly Tonic helps individuals achieve sustainable weight loss results.

  3. Puravive doesn’t just help you lose weight; it also boosts your metabolism, turning your body into a fat-burning machine. This means you not only lose weight but also feel more energetic and lively.

  4. Sugar Defender transcends symptom-focused interventions, delving into the root causes of glucose imbalance. It stands as a dynamic formula that seamlessly aligns with the body’s intrinsic mechanisms, presenting a distinctive and holistic approach to enhancing overall well-being. Beyond a mere supplement, Sugar Defender emerges as a strategic ally in the pursuit of balanced health.

  5. I appreciate how this blog promotes self-love and self-care It’s important to prioritize our well-being and your blog reminds me of that

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注