-
[LeetCode] Fizz Buzz알고리즘 2022. 4. 23. 20:28
[LeetCode] Fizz Buzz
풀이_#1
반복문과 조건문 활용
class Solution(object): def fizzBuzz(self, n): """ :type n: int :rtype: List[str] """ r = []; for i in range(1, n+1): s = str(i); if (i % 3 == 0 and i % 5 == 0): s = "FizzBuzz" elif (i % 3 == 0): s = "Fizz" elif (i % 5 == 0): s = "Buzz" r.append(s) return r
풀이_#2
리스트 이해 응용
class Solution(object): def fizzBuzz(self, n): """ :type n: int :rtype: List[str] """ return ["Fizz" * (not i % 3) + "Buzz" * (not i % 5) or str(i) for i in range(1,n+1)]
'알고리즘' 카테고리의 다른 글
[LeetCode] The K Weakest Rows in Matrix (0) 2022.04.23 [LeetCode] Middle of the Linked List (0) 2022.04.23 [백준] 최단 경로 (0) 2022.03.01 [프로그래머스] 가장 먼 노드 (0) 2022.02.10 [백준] 승부예측 (파이썬) (0) 2022.02.04