golang solution


  • 0
    M
    func isHappy(n int) bool {    
    	hash := make(map[int]bool)
    
    	for {
    		res := 0
    
    		for n > 0 {
    			num := n % 10
    			res = res + num*num
    			n = n / 10
    		}
    
    		if res == 1 {
    			return true
    		}
    
    		_, ok := hash[res]
    
    		if ok {
    			return false
    		}
    
    		hash[res] = true
    		n = res
    	}
    }
    

Log in to reply
 

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