saurus2
Saurus2
saurus2
전체 방문자
오늘
어제
  • 분류 전체보기
    • 개발
      • AJAX
    • ML Ops
    • Profile
    • 음식점
    • 배낭여행
    • 컴퓨터공학
      • 알고리즘 공부
      • C++
      • Sever 스터디
      • Java spring
      • 알고리즘 _ 문제해결
      • 딥러닝
      • Java 정리
      • Python
      • LeetCode 1000
      • Machine Learning Study
      • Sign language Detection Pro..
      • LeetCode Solutions
    • 비콘
    • 데일리 리포트
    • 유학일기
      • 영어 공부
      • Daily
    • AI Master Degree
      • Data Mining
      • AI and Data engineering
      • Math Foundations for Decisi..
      • Natural Language Processing

블로그 메뉴

  • 홈
  • 태그
  • 미디어로그
  • 위치로그
  • 방명록

공지사항

인기 글

태그

  • c++
  • Python
  • 백준
  • BFS
  • 개발자
  • 파이썬
  • 온라인저지
  • 문제해결능력
  • 알고리즘
  • 취준
  • two pointer
  • DFS
  • 딕셔너리
  • 개발자 취업준비
  • 딥러닝
  • 취업준비
  • LeetCode
  • 릿코드
  • 알고리즘문제해결
  • 리트코드

최근 댓글

최근 글

티스토리

hELLO · Designed By 정상우.
saurus2

Saurus2

컴퓨터공학/LeetCode Solutions

[LeetCode] 412. Fizz Buzz

2022. 12. 30. 14:55

412. Fizz Buzz

Easy

Given an integer n, return a string array answer (1-indexed) where:

  • answer[i] == "FizzBuzz" if i is divisible by 3 and 5.
  • answer[i] == "Fizz" if i is divisible by 3.
  • answer[i] == "Buzz" if i is divisible by 5.
  • answer[i] == i (as a string) if none of the above conditions are true.

 

Example 1:

Input: n = 3
Output: ["1","2","Fizz"]

Example 2:

Input: n = 5
Output: ["1","2","Fizz","4","Buzz"]

Example 3:

Input: n = 15
Output: ["1","2","Fizz","4","Buzz","Fizz","7","8","Fizz","Buzz","11","Fizz","13","14","FizzBuzz"]

 

Constraints:

  • 1 <= n <= 104

문제 풀이

  • n이 주어진고 1부터 n까지 숫자를 판별해 정답을 저장하는 문제이다.
  • 해당 숫자가 3과 5의 공약수라면 'FizzBuzz' 이어야한다.
  • 단지 3의 공약수라면 'Fizz', 5의 공약수라면 'Buzz'를 지정해야한다.
  • 나머지는 숫자를 문자로 변경하여 저장한다. 
  • 제한사항은 10^4이기 때문에 브루투 포스로 풀어도된다. 
  • 이러한 문제들은 시뮬레이션 문제이며, 주어진 조건들로 어떻게 논리적으로 코드를 짤 수 있는지를 증명하는 문제이다.
  • 물론 공약수 문제이기 때문에 수학공식을 사용하며 문제를 빠르게 풀수도 있지만 정석으로 풀면
  • for문을 사용하여 1 부터 n 까지 탐색한다. 
  • 숫자를 나눠서 각각 숫자들의 공약수에 해당하는 정답을 넣는다. 

소스 코드

class Solution:
    def fizzBuzz(self, n: int) -> List[str]:
        answer = []
        for i in range(1, n+1):
            if i % 3 == 0 and i % 5 == 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
저작자표시 (새창열림)

'컴퓨터공학 > LeetCode Solutions' 카테고리의 다른 글

[LeetCode] 242. Valid Anagram  (0) 2023.01.10
[LeetCode] 20. Valid Parentheses  (0) 2023.01.06
[LeetCode] 290. Word Pattern  (0) 2022.12.30
[LeetCode] 217. Contains Duplicate  (0) 2022.12.30
[LeetCode] 344. Reverse String  (0) 2022.12.30
    '컴퓨터공학/LeetCode Solutions' 카테고리의 다른 글
    • [LeetCode] 242. Valid Anagram
    • [LeetCode] 20. Valid Parentheses
    • [LeetCode] 290. Word Pattern
    • [LeetCode] 217. Contains Duplicate
    saurus2
    saurus2
    Simple is Best

    티스토리툴바