ruby simple solution


  • 0
    T
    def is_happy(n)
        seen = {}
        i = 0
        n = n.to_s
        while n != "1" && seen[n].nil? && i < 100
            seen[n] = true
            n = n.each_char.reduce(0) { |m, n| m + n.to_i**2 }.to_s
            i += 1
        end
        return n == "1"
    end
    

Log in to reply
 

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