-
Notifications
You must be signed in to change notification settings - Fork 112
/
0412-FizzBuzz.cs
36 lines (30 loc) · 984 Bytes
/
0412-FizzBuzz.cs
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
//-----------------------------------------------------------------------------
// Runtime: 216ms
// Memory Usage: 33.5 MB
// Link: https://leetcode.com/submissions/detail/335617325/
//-----------------------------------------------------------------------------
using System.Collections.Generic;
namespace LeetCode
{
public class _0412_FizzBuzz
{
public IList<string> FizzBuzz(int n)
{
var answer = new List<string>();
for (int num = 1; num <= n; num++)
{
var divisibleBy3 = (num % 3 == 0);
var divisibleBy5 = (num % 5 == 0);
var str = string.Empty;
if (divisibleBy3)
str += "Fizz";
if (divisibleBy5)
str += "Buzz";
if (string.IsNullOrEmpty(str))
str = num.ToString();
answer.Add(str);
}
return answer;
}
}
}