LeetCode 0258 - Add Digits

2021-08-11 12:07:20 浏览数 (2)

Add Digits

Desicription

Given a non-negative integer num, repeatedly add all its digits until the result has only one digit.

Example:

代码语言:javascript复制
Input: 38
Output: 2 
Explanation: The process is like: 3   8 = 11, 1   1 = 2. 
             Since 2 has only one digit, return it.

Solution

代码语言:javascript复制
class Solution {
private:
    int getLength(int num) {
        return static_cast<int>(log10(num)   1);
    }
public:
    int addDigits(int num) {
        if(getLength(num) <= 1) {
            return num;
        }
        int sum = 0;
        while(num) {
            sum  = num % 10;
            num /= 10;
        }
        return addDigits(sum);
    }
};

0 人点赞