3ms Java solution


  • 1
    J
    public class Solution {
        public int addDigits(int num) {
            if(num / 10 == 0){
                return num;
            }
            int result = 0;
            while(true){
                result += num % 10 ;
                num = num/10;
                if( num == 0 ){
                    if(result / 10 == 0){
                        return result;
                    }
                    num = result;
                    result = 0;
                }
            }
           
            
        }
    }

  • -2
    A

    if(num>=10)
    {
    if(num%9==0)
    {
    return 9;
    }
    return num%9;
    }
    else
    {
    return num;
    }


Log in to reply
 

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