func MyAtoi(s string) int { //fmt.Printf("input:%s\n", s) var arrString = []rune(s) lenArr := len(arrString) if lenArr == 0 { return 0 } //查找第一个索引 firstIndex := 0 for j := 0; j < lenArr; j++ { if arrString[j] != 32 { break } firstIndex++ } //fmt.Printf("firstIndex:%d\n", firstIndex) fuNum := false //是否含有符号 firstChar := string(arrString[firstIndex]) if firstChar == "-" { fuNum = true } if firstChar == "-" || firstChar == "+" { firstIndex++ } //fmt.Printf("firstIndex:%d\n", firstIndex) var result int result = 0 yuejie := false for i := firstIndex; i < lenArr; i++ { //fmt.Println(string(arrString[i])) if arrString[i] < 48 { //fmt.Println("aaa") break } if arrString[i] > 57 { //fmt.Println("bbb") break } result = result*10 + (int(arrString[i]) - 48) if result > math.MaxInt32 { result = math.MaxInt32 yuejie = true } } if fuNum { if yuejie { result = 0 - result - 1 } else { result = 0 - result } } //fmt.Printf("output:%d\n", result) return result }
标签:++,fmt,arrString,lenArr,整型,字符串,firstIndex,result From: https://www.cnblogs.com/zixiaofeixian/p/16998114.html