-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfizz-buzz.cpp
More file actions
33 lines (28 loc) · 746 Bytes
/
fizz-buzz.cpp
File metadata and controls
33 lines (28 loc) · 746 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
class Solution {
public:
vector<string> fizzBuzz(int n) {
vector<string> result;
if (n <= 0) return result;
for (int i = 1; i <= n; i++) {
if (aliquot3_5(i)) {
result.push_back("FizzBuzz");
} else if (aliquot3(i)) {
result.push_back("Fizz");
} else if (aliquot5(i)) {
result.push_back("Buzz");
} else {
result.push_back(to_string(i));
}
}
return result;
}
bool aliquot3(int num) {
return num % 3 == 0;
}
bool aliquot5(int num) {
return num % 5 == 0;
}
bool aliquot3_5(int num) {
return num % 3 == 0 && num % 5 == 0;
}
};