LeetCode 258 Add Digits

Question

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

Submission

Find the digital root of any positive number. The solution appear periodically from 1 to 9.

Java Submission
1
2
3
4
5
public class Solution {
public int addDigits(int num) {
return (num - 1) % 9 + 1;
}
}

Reference

  1. Digital root https://en.wikipedia.org/wiki/Digital_root