题目:
题解:
func myAtoi(s string) int {abs, sign, i, n := 0, 1, 0, len(s)//丢弃无用的前导空格for i < n && s[i] == ' ' {i++}//标记正负号if i < n {if s[i] == '-' {sign = -1i++} else if s[i] == '+' {sign = 1i++}}for i < n && s[i] >= '0' && s[i] <= '9' {abs = 10*abs + int(s[i]-'0') //字节 byte '0' == 48if sign*abs < math.MinInt32 { //整数超过 32 位有符号整数范围return math.MinInt32} else if sign*abs > math.MaxInt32 {return math.MaxInt32}i++}return sign * abs
}