-
Notifications
You must be signed in to change notification settings - Fork 56
Expand file tree
/
Copy pathFizzBuzz.cpp
More file actions
45 lines (24 loc) · 857 Bytes
/
FizzBuzz.cpp
File metadata and controls
45 lines (24 loc) · 857 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
32
33
34
35
36
37
38
39
40
/*
https://www.interviewbit.com/problems/fizzbuzz/
Given a positive integer N, print all the integers from 1 to N. But for multiples of 3 print “Fizz” instead of the number and for the multiples of 5 print “Buzz”. Also for number which are multiple of 3 and 5, prints “FizzBuzz”.
Example
N = 5
Return: [1 2 Fizz 4 Buzz]
Note: Instead of printing the answer, you have to return it as list of strings.
*/
vector<string> Solution::fizzBuzz(int A)
{
vector<string> v;
for(auto i=1; i<=A; i++)
{
if(i%3==0 && i%5==0)
v.push_back("FizzBuzz");
else if(i%3==0)
v.push_back("Fizz");
else if(i%5==0)
v.push_back("Buzz");
else
v.push_back(to_string(i));
}
return v; // O(n), O(n)
}