-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.cs
More file actions
33 lines (31 loc) · 928 Bytes
/
Solution.cs
File metadata and controls
33 lines (31 loc) · 928 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
namespace LeetCode.Problem412.Alternative{
//412. Fizz Buzz
//https://leetcode.com/problems/fizz-buzz/
/*
Given an integer n, return a string array answer (1-indexed) where:
answer[i] == "FizzBuzz" if i is divisible by 3 and 5.
answer[i] == "Fizz" if i is divisible by 3.
answer[i] == "Buzz" if i is divisible by 5.
answer[i] == i (as a string) if none of the above conditions are true.
*/
public class Solution {
public IList<string> FizzBuzz(int n) {
List<string> ans = new();
for (int i = 1; i <= n; i++)
{
if (!(i % 3 == 0 || i % 5 == 0))
ans.Add(i.ToString());
else
{
if ((i % 3 == 0) && (i % 5 == 0))
ans.Add("FizzBuzz");
else
{
ans.Add(i % 3 == 0 ? "Fizz" : "Buzz");
}
}
}
return ans;
}
}
}