Notice
Recent Posts
Recent Comments
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 |
Tags
- 더현대서울 맛집
- Kaggle
- PYTHON
- SW Expert Academy
- 프로그래머스
- 편스토랑
- 캐치카페
- 맥북
- AI 경진대회
- programmers
- hackerrank
- Git
- Docker
- 금융문자분석경진대회
- Baekjoon
- ChatGPT
- Real or Not? NLP with Disaster Tweets
- 데이콘
- 파이썬
- gs25
- 편스토랑 우승상품
- 자연어처리
- 백준
- 프로그래머스 파이썬
- dacon
- 코로나19
- leetcode
- 우분투
- github
- ubuntu
Archives
- Today
- Total
솜씨좋은장씨
[leetCode] 258. Add Digits (Python) 본문
728x90
반응형

Given a non-negative integer num, repeatedly add all its digits until the result has only one digit.
Example:
Input: 38
Output: 2
Explanation: The process is like: 3 + 8 = 11, 1 + 1 = 2.
Since 2 has only one digit, return it.
Follow up:
Could you do it without any loop/recursion in O(1) runtime?
Solution
class Solution:
def addDigits(self, num: int) -> int:
while True:
if num // 10 < 1:
return num
num_list = list(map(int, list(str(num))))
sum_num = sum(num_list)
num = sum_num



SOMJANG/CODINGTEST_PRACTICE
1일 1문제 since 2020.02.07. Contribute to SOMJANG/CODINGTEST_PRACTICE development by creating an account on GitHub.
github.com
'Programming > 코딩 1일 1문제' 카테고리의 다른 글
[leetCode] 1470. Shuffle the Array (Python) (0) | 2020.10.10 |
---|---|
[leetCode] 896. Monotonic Array (Python) (0) | 2020.10.09 |
[leetCode] 1089. Duplicate Zeros (Python) (0) | 2020.10.07 |
[leetCode] 17. Letter Combinations of a Phone Number (Python) (0) | 2020.10.06 |
[leetCode] 350. Intersection of Two Arrays II (Python) (0) | 2020.10.05 |