Solution in GoLang


  • 0
    N

    I don't know why the parameter of Golang section must be int. It doesn't matter because I corrected in my code.
    I'm practicing Golang although I wrote in C++ in very early thread of the problem like 4 years ago.

    func reverse(x int) int {
    	y := int32(x)    // I made this stupid variable because of the deveopers in Leetcode
    // The problem requests 32-bit int although this golang requests 64-bit int param.
    
    	const MAX_INT = int32(^uint32(0) >> 1)
    	const MIN_INT = -MAX_INT - 1
    
    	var ret int32 = 0
    	for y != 0 {
    		ret = 10*ret + (y % 10)
    		y /= 10
    		if y < 0 && (ret < MIN_INT/10 || (ret == MIN_INT/10 && y < MIN_INT%10)) {
    			return 0
    		}
    		if y > 0 && (ret > MAX_INT/10 || (ret == MAX_INT/10 && y > MAX_INT%10)) {
    			return 0
    		}
    	}
    	return int(ret)    // SO stupid
    }
    

Log in to reply
 

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