Simple to understand Swift Solution


  • 0
    T
    func isUgly(_ num: Int) -> Bool {
            guard num > 0 else {
                return false
            }    
            
            var n = num
            
            while n > 1 {
                if n % 5 == 0 {
                    n = n / 5
                } else if n % 3 == 0 {
                    n = n / 3
                } else if n % 2 == 0 {
                    n = n / 2
                } else {
                    return false
                }
            }
            
            return true
        }
    

Log in to reply
 

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