Swift solution - O(n) time


  • 0
    class Solution {
        func fizzBuzz(_ n: Int) -> [String] {
            if n < 1 {
                return []
            }
            
            var result = [String]()
            
            for i in 1...n {
                if i % (3 * 5) == 0 {
                    result.append("FizzBuzz")
                } else if i % 3 == 0 {
                    result.append("Fizz")
                } else if i % 5 == 0 {
                    result.append("Buzz")
                } else {
                    result.append(String(i))
                }
            }
            
            return result
        }
    }
    

Log in to reply
 

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