Hyoseo Lee
01 Index 02 Current page Writing 03 Work 04 Notes 05 Games 06 Author
leetcode

258. Add Digits

Topic Math
Area Algorithms
Summary
if it is single digit, return it. or, do the same process.

Problem

View on LeetCode →

Difficulty: Easy
Tags: Math, Simulation, Number Theory

Intuition

this one is easy. simple recursion question.

Approach

if it is single digit, return it. or, do the same process.

Solution

int addDigits(int num) {
    int tmp;
    if(num < 10){
        return num;
    }
    else{
        while(num > 0){
            tmp += num % 10;
            num = num/10;
        }
        return addDigits(tmp);
    }
}

Complexity

  • Time: don’t know really.

  • Space: don’t know really. proportional to time it repeated.

Thoughts

this one was way to easy.