public class Solution { public IList<string> FizzBuzz(int n) { var list = new List<string>(); for (int i = 1; i <= n; i++) { if (i % 15 == 0) { list.Add("FizzBuzz"); } else if (i % 5 == 0) { list.Add("Buzz"); } else if (i % 3 == 0) { list.Add("Fizz"); } else { list.Add(i.ToString()); } } return list; } }
https://leetcode.com/problems/fizz-buzz/#/description
C++代码:
class Solution { public: vector<string> fizzBuzz(int n) { vector<string> V; for (int i = 1; i <= n; 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 { stringstream stream; stream << i; V.push_back(stream.str()); } } return V; } };