[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 |