-
Notifications
You must be signed in to change notification settings - Fork 0
/
fizz-buzz.cpp
39 lines (39 loc) · 843 Bytes
/
fizz-buzz.cpp
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
#define pb push_back
class Solution {
public:
string int_to_string(int n) //will not return correct string if the num<=0
{
string ans;
while(n)
{
int dig = n%10;
ans += ('0' + dig);
n /= 10;
}
reverse(ans.begin(),ans.end());
return ans;
}
vector<string> fizzBuzz(int n) {
vector<string> ans;
for(int i=1;i<=n;i++)
{
if(i%3==0 && i%5==0)
{
ans.pb("FizzBuzz");
}
else if(i%3 == 0)
{
ans.pb("Fizz");
}
else if(i%5 == 0)
{
ans.pb("Buzz");
}
else
{
ans.pb(int_to_string(i));
}
}
return ans;
}
};