258. Add Digits
Topic Math
Area Algorithms
Summary
if it is single digit, return it. or, do the same process.
Problem
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.