O(1) Final Solution for Positive and Negative numbers (Java)


  • 0
    X
    public int addDigits(int num) {
        if (num >= 0)
            return 1 + (num - 1) % 9;
        else
            return Math.abs(-1 + (num + 1) % 9);
    }
    

    It's already published by other people, but it's under comments, so I update the final solution here.


Log in to reply
 

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