Swift solution using a switch statement


  • 0
    K
    class Solution {
        func fizzBuzz(_ n: Int) -> [String] {
            var ret: [String] = [] 
            
            for num in 1...n {
                ret.append(process(num))
            }
            
            return ret
        }
        
        func process(_ n: Int) -> String {
            switch (n % 3 == 0, n % 5 == 0) {
                case (true, false):
                    return "Fizz"
                case (false, true):
                    return "Buzz"
                case (true, true):
                    return "FizzBuzz"
                case (false, false):
                    return "\(n)"
            }
        }
    }
    

Log in to reply
 

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