Posted on

Description

Submission

class Solution {
public:
    int addDigits(int num) {
        if(num / 10 == 0) return num;
        int ret = 0;
        for(; num != 0; num /= 10) {
            ret += (num % 10);
        }
        return addDigits(ret);
    }
};

Submission 220303

class Solution {
public:
    int addDigits(int num) {
        while(num>9) {
            int cur = 0;
            for(; num; num /= 10) {
                cur += num % 10;
            }
            num = cur;
        }
        return num;
    }
};

Leave a Reply

Your email address will not be published. Required fields are marked *