Easiest solution O(N), memory - O(1).


  • 2
    D

    Easiest solution O(N), memory - O(1).

    public class Solution {
    public int addDigits(int num) {
        int sum=0;
        do{ sum+=num%10;
            if(sum>9) 
                sum = sum-10+1;
            num/=10;
        }while(num!=0);
        return sum;
    }
    

    }


Log in to reply
 

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