forked from soapyigu/LeetCode-Swift
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFizzBuzz.swift
More file actions
31 lines (27 loc) · 716 Bytes
/
FizzBuzz.swift
File metadata and controls
31 lines (27 loc) · 716 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
/**
* Question Link: https://leetcode.com/problems/fizz-buzz/
* Primary idea: Iterate the array and handle multiples of 3 or 5 separately.
*
* Time Complexity: O(n), Space Complexity: O(1)
*
*/
class FizzBuzz {
func fizzBuzz(_ n: Int) -> [String] {
var res = [String]()
if n < 0 {
return res
}
for i in 1...n {
if i % 3 == 0 && i % 5 == 0 {
res.append("FizzBuzz")
} else if i % 3 == 0 {
res.append("Fizz")
} else if i % 5 == 0 {
res.append("Buzz")
} else {
res.append("\(i)")
}
}
return res
}
}