forked from KnowledgeCenterYoutube/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
412_Fizz_Buzz
54 lines (43 loc) · 1.32 KB
/
412_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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
Leetcode 412: Fizz Buzz
Detailed video explanation: https://youtu.be/13gtlfrR6L8
===========================================
C++:
----
class Solution {
public:
vector<string> fizzBuzz(int n) {
vector<string> result(n, "");
for(int i = 1; i < n+1; ++i){
if(i % 15 == 0) result[i-1] = "FizzBuzz";
else if (i % 5 == 0) result[i-1] = "Buzz";
else if (i % 3 == 0) result[i-1] = "Fizz";
else result[i-1] = to_string(i);
}
return result;
}
};
Java:
-----
class Solution {
public List<String> fizzBuzz(int n) {
String[] result = new String[n];
for(int i = 1; i < n+1; ++i){
if(i % 15 == 0) result[i-1] = "FizzBuzz";
else if (i % 5 == 0) result[i-1] = "Buzz";
else if (i % 3 == 0) result[i-1] = "Fizz";
else result[i-1] = Integer.toString(i);
}
return Arrays.asList(result);
}
}
Python3:
-------
class Solution:
def fizzBuzz(self, n: int) -> List[str]:
result = [None]*n
for i in range(1, n+1):
if i % 15 == 0: result[i-1] = "FizzBuzz"
elif i % 5 == 0: result[i-1] = "Buzz"
elif i % 3 == 0: result[i-1] = "Fizz"
else: result[i-1] = str(i)
return result