HelloMinchan
처음처럼
HelloMinchan
LinkedIn
전체 방문자
오늘
어제
  • 분류 전체보기 (306)
    • Backend (4)
      • NestJS (1)
      • Express (1)
      • Spring (2)
    • Infrastructure (1)
      • AWS (1)
    • Frontend (1)
      • Next.js (1)
    • Language & Runtime (4)
      • Java (2)
      • Node.js (2)
    • Computer Science (8)
      • Computer Networks (3)
      • Operating Systems (4)
      • OOP (1)
    • 독서 (4)
      • 데이터 중심 애플리케이션 설계 (3)
      • 객체지향의 사실과 오해 (1)
    • 회고 (4)
      • Project (2)
      • Career (2)
    • Deprecated (280)

채널

  • GitHub
  • LinkedIn

최근 글

태그

  • front-end
  • 알고스팟
  • Algospot
  • 백준C++
  • 백엔드
  • 프로그래머스
  • 백준Go
  • 백준Python
  • programmers
  • 알고스팟Python
  • 프로그래밍
  • Database
  • 백준
  • back-end
  • Baekjoon Online Judge
  • 개발자
  • 코딩
  • 프로그래머스C++
  • 프로그래머스Python
  • 데이터베이스

최근 댓글

인기 글

hELLO
HelloMinchan

처음처럼

[LeetCode] 리트코드 1342번 Number of Steps to Reduce a Number to Zero(Python)
Deprecated

[LeetCode] 리트코드 1342번 Number of Steps to Reduce a Number to Zero(Python)

2020. 3. 3. 23:59

Copyright © 2020 LeetCode

[LeetCode] 리트코드 1342번 Number of Steps to Reduce a Number to Zero

(Python)

(글쓴날 : 2020.03.03)

 


* LeetCode, 리트코드 1342번 문제 Python 언어 풀이입니다.

* 소스 코드의 저작권은 글쓴이에게 있습니다.


 

 

리트코드 1342번 Number of Steps to Reduce a Number to Zero


1) 문제

문제 링크 : https://leetcode.com/problems/number-of-steps-to-reduce-a-number-to-zero/

 

Number of Steps to Reduce a Number to Zero - 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


2) 풀이 과정

주어지는 숫자를 특정한 규칙을 통해 0으로 만드는 데까지 걸리는 횟수를 구하는 문제입니다.

규칙으로는 주어진 숫자가 짝수일 시 2로 나누고 홀수일 시 1을 빼는 것이며, 최종적으로 0이 될 때까지 연산한 횟수를 반복문을 사용해 구해주시면 되겠습니다.

 

리트코드의 경우, 구한 값을 출력하는 것이 아니라 반환하는 형태이므로 위에서 구한 값을 return문으로 반환해 주시면 되겠습니다.


3) 코드

 

* Python 코드

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class Solution:
 
    def numberOfSteps (self, num: int) -> int:
        count = 0
        while True:
            if num == 0:
                break
            elif num % 2 == 0:
                count += 1
                num = num / 2
            else:
                count += 1
                num = num - 1
 
        return count

 

저작자표시 비영리 변경금지 (새창열림)

'Deprecated' 카테고리의 다른 글

[MySQL] 데이터베이스(스키마) 생성, 조회, 사용, 삭제하는 법  (0) 2020.03.06
[LeetCode] 리트코드 1365번 How Many Numbers Are Smaller Than the Current Number(Python)  (0) 2020.03.04
[CSS] flexbox를 이용한 레이아웃(이미지, div 등) 가운데 정렬하는 법  (0) 2020.03.02
[CSS] 레이아웃 height 100%로 동작하게 하는 법  (2) 2020.03.01
[Baekjoon Online Judge] 백준 1065번 한수(Python)  (2) 2020.02.29
    'Deprecated' 카테고리의 다른 글
    • [MySQL] 데이터베이스(스키마) 생성, 조회, 사용, 삭제하는 법
    • [LeetCode] 리트코드 1365번 How Many Numbers Are Smaller Than the Current Number(Python)
    • [CSS] flexbox를 이용한 레이아웃(이미지, div 등) 가운데 정렬하는 법
    • [CSS] 레이아웃 height 100%로 동작하게 하는 법
    HelloMinchan
    HelloMinchan
    Though you should not fear failure, You should do your very best to avoid it.

    티스토리툴바