Golang 6ms concise solution


  • 0

    It's still slow (beats only 37.5% of other submissions).
    Please let me know if you have some idea to optimize this code.

    func myAtoi(str string) int {
    	if str == "" {
    		return 0
    	}
    
    	i := 0
    	for str[i] == ' ' {
    		i++
    	}
    
    	positive := true
    	if str[i] == '+' {
    		i++
    	} else if str[i] == '-' {
    		i++
    		positive = false
    	}
    
    	var res int
    	for ; i < len(str); i++ {
    		diff := int(str[i] - '0')
    		if diff < 0 || diff > 9 {
    			break
    		}
    
    		if positive {
    			if res > (math.MaxInt32-diff)/10 {
    				return math.MaxInt32
    			}
    			res = res*10 + diff
    			continue
    		}
    		if res < (math.MinInt32+diff)/10 {
    			return math.MinInt32
    		}
    		res = res*10 - diff
    	}
    	return res
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.