Add Digits
-
[LeetCode/Java] 258. Add Digits - A0232022_PPS/2주차 2023. 1. 3. 21:47
- 문제 Given an integer num, repeatedly add all its digits until the result has only one digit, and return it. - 알고리즘 숫자의 길이를 파악하여, 1자리 숫자가 될때까지 반복한다. 그리고 마지막 남은 수를 return 한다. - 코드 class Solution { public int addDigits(int num) { int len = (int)(Math.log10(num)+1); int count = 0; while(len > 1){ int result = 0; for(int i = 0; i