标签:accordion label enum 规则 www tags lintcode text basic
给你一个整数n. 从 1 到 n 按照下面的规则打印每个数:
fizz
.buzz
.3
和5
整除,打印fizz buzz
.比如 n = 15
, 返回一个字符串数组:
[
"1", "2", "fizz",
"4", "buzz", "fizz",
"7", "8", "fizz",
"buzz", "11", "fizz",
"13", "14", "fizz buzz"
]
1 class Solution { 2 public: 3 vector<string> fizzBuzz(int n) { 4 vector<string> result; 5 for (int i = 1; i <= n; i++) { 6 if (i % 15 == 0) { 7 result.push_back("fizz buzz"); 8 } else if (i % 3 == 0) { 9 result.push_back("fizz"); 10 } else if (i % 5 == 0) { 11 result.push_back("buzz"); 12 } else { 13 result.push_back(to_string(i)); 14 } 15 } 16 return result; 17 } 18 };
标签:accordion label enum 规则 www tags lintcode text basic
原文地址:http://www.cnblogs.com/libaoquan/p/6795269.html