My solution in 2ms. Is it ok to do like this?


  • 0
    C
    public class Solution {
        public int addDigits(int num) {
            int sum = 0;
            while ( num > 0 || sum > 9) {
                   sum += num%10;
                   num /=10;
                   if(sum > 9 && num ==0){
                        num = sum;
                        sum = 0;
                   }
                }
            return sum;
       }
    }

  • 0
    M

    hmmmm,you seem to have ignored the follow up which requires "no loop/recursion"...


  • 0
    S

    It doesn't require no loop, it just hints that it's possible.


Log in to reply
 

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