FizzBuzz Solution (Swift)


  • 0
    X

    It is easy, if you know the basic knowledge of swift!

    But I got a problem using result[i] = "some", it reports
    index out of range error.

    I tried it in playground, but ...

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

Log in to reply
 

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