문제/릿코드

[릿코드/LeetCode] 412. Fizz Buzz (파이썬3/Python3)

개 살구 2021. 7. 18. 20:05

문제

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


생각

우선 리스트를 증가하는 숫자로 채워놓고

Fizz와 Buzz들어갈 부분만 바꿔주면 될 듯?

나는 그냥 for문 돌려서 조건 맞으면 값 바꿔줘야겠다. 

-

그냥 바로 for문 돌리면서 append해도 굿!


코드

class Solution:
    def fizzBuzz(self, n: int) -> List[str]:
        R = [str(i+1) for i in range(n)]
        for i in range(n):
            if (i+1) % 3 == 0 and (i+1) % 5 == 0:
                R[i] = "FizzBuzz"
            elif (i+1) % 3 == 0:
                R[i] = "Fizz"
            elif (i+1) % 5 == 0:
                R[i] = "Buzz"
        return R