力扣题-12.9
力扣题1:412. Fizz Buzz
解题思想:直接遍历添加至answer即可
class Solution(object):
def fizzBuzz(self, n):
"""
:type n: int
:rtype: List[str]
"""
answer = []
for i in range(1,n+1):
if i%15 ==0:
answer.append('FizzBuzz')
elif i%3 == 0:
answer.append('Fizz')
elif i%5 ==0:
answer.append('Buzz')
else:
answer.append(str(i))
return answer
class Solution {
public:
vector<string> fizzBuzz(int n) {
std::vector<std::string> answer(n);
for(int i=1;i<=n;i++){
if(i%15==0){
answer[i-1]="FizzBuzz";
}
else if(i%3==0){
answer[i-1]="Fizz";
}
else if(i%5==0){
answer[i-1]="Buzz";
}
else{
answer[i-1]=std::to_string(i);;
}
}
return answer;
}
};