力扣-412.Fizz Buzz

AC Code

class Solution {
public:
    vector<string> fizzBuzz(int n) {
        vector<string> ans;
        for(int i = 1; i <= n; i++) {
            if(i % 3 == 0 && i % 5 == 0) {
                ans.emplace_back("FizzBuzz");
                continue;
            }
            if(i % 3 == 0) {
                ans.emplace_back("Fizz");
                continue;
            }
            if(i % 5 == 0) {
                ans.emplace_back("Buzz");
                continue;
            }
            ans.emplace_back(to_string(i));
        }
        return ans;
    }
};

力扣-412.Fizz Buzz_第1张图片

你可能感兴趣的:(LeetCode,leetcode,算法)