class Solution {
public:
vector<string> fizzBuzz(int n) {
vector<string> answer;
for(int i = 1; i <= n; i++) {
if(i % 3 == 0 && i % 5 == 0) answer.push_back("FizzBuzz");
else if(i % 3 == 0) answer.push_back("Fizz");
else if(i % 5 == 0) answer.push_back("Buzz");
else answer.push_back(to_string(i));
}
return answer;
}
};
3으로 나누어 떨어지면 'Fizz' 5로 나누어 떨어지면 'Buzz' 3과 5로 나누어 떨어지면 'FizzBuzz' 를 출력하는 문제인데
3과 5로 동시에 나누어 떨어지면 FizzBuzz를 출력하고 3가지가 모두 아니라면 숫자를 출력하도록 하였습니다.
문제: https://leetcode.com/problems/fizz-buzz/
Fizz Buzz - LeetCode
Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.
leetcode.com
'Problem Solving > 리트코드(leetcode)' 카테고리의 다른 글
[Leetcode/C++] 509. Fibonacci Number (0) | 2022.01.28 |
---|---|
[Leetcode/C++] 125. Valid Palindrome (0) | 2022.01.28 |
[Leetcode/C++] 1154. Day of the Year (0) | 2022.01.28 |
[Leetcode/C++] 1047. Remove All Adjacent Duplicates In String (0) | 2022.01.26 |
[Leetcode/C++] 1051. Height Checker (0) | 2022.01.24 |