-
Notifications
You must be signed in to change notification settings - Fork 0
/
Fizz Buzz
39 lines (36 loc) · 893 Bytes
/
Fizz Buzz
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
32
33
34
35
36
37
38
39
Python
class Solution:
def fizzBuzz(self, n: int) -> List[str]:
res = []
if n == 0:
return res
for i in range(n):
if (i+1) % 15 == 0:
res.append('FizzBuzz')
elif (i+1) % 5 == 0:
res.append('Buzz')
elif (i+1) % 3 == 0:
res.append('Fizz')
else:
res.append(str(i+1))
return res
C++
class Solution
{
public:
vector<string> fizzBuzz(int n)
{
vector<string> res;
for (int i=1; i<=n; i++)
{
if (i % 15 == 0)
res.push_back("FizzBuzz");
elif (i % 5 == 0)
res.push_back("Buzz");
elif (i % 3 == 0)
res.push_back("Fizz");
else
res.push_back(to_string(i));
}
return res;
}