1-line solution with O(1) in Java

  • 1
    public class Solution {
        public int addDigits(int num) {
                return num<10? num: (num%9==0? 9: num%9);

  • 0

    The result can only be 0-9. The remainder of the number divided by 9 is exactly the sum of the digits, except when the remainder is 0, the sum should be 9.

Log in to reply

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.