0ms Solution.Correct me if I missed any boundary case


  • 5
    S

    public class Solution {
    public int addDigits(int num) {

        if(num<10){
            return num;
        }
        else{
            return 1+((num-1)%9);
        }
        
    }
    

    }


  • 1
    M

    Simple C solution

    int addDigits(int num) {
        if(num==0)
            return 0;
        else if(num%9==0)
            return 9;
        else
            return num%9;
    }
    

    Obviously,if the number provided is 0,the required answer is 0.And if the number provided is a multiple of 9,the required answer is 9,you can try that and the last case is if the number provided does not satisfy the above cases,then the required answer is (provided number%9).


  • 0
    Y

    My solution is pretty much the same as yours and it cost 8ms. How about yours?

    class Solution {
    public:
    int addDigits(int num) {
    int remainder;
    if(num == 0)
    return 0;
    else if ((remainder = num % 9) == 0)
    return 9;
    else
    return remainder;
    }
    };


Log in to reply
 

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