반응형
https://leetcode.com/problems/add-digits/description/
Add Digits - LeetCode
Can you solve this real interview question? Add Digits - Given an integer num, repeatedly add all its digits until the result has only one digit, and return it. Example 1: Input: num = 38 Output: 2 Explanation: The process is 38 --> 3 + 8 --> 11 11 -->
leetcode.com
반복문을 이용하여 num이 한 자릿수가 될 때까지 연산을 수행하였습니다.
class Solution {
public:
int addDigits(int num) {
int res = num;
while(res >= 10) {
int s = 0;
while(res) {
s+=res%10, res/=10;
}
res = s;
}
return res;
}
};
반응형
'Algorithm' 카테고리의 다른 글
LeetCode 1697. Checking Existence of Edge Length Limited Paths (1) | 2023.04.29 |
---|---|
LeetCode 839. Similar String Groups (0) | 2023.04.29 |
LeetCode 2336. Smallest Number in Infinite Set (0) | 2023.04.27 |
LeetCode 1046. Last Stone Weight (0) | 2023.04.24 |
LeetCode 1416. Restore The Array (0) | 2023.04.23 |