how to improve


  • 0
    D

    in this way , the answer beats 40.09%.
    Is there any better solution?
    or how can I improve my code?
    thanks in advance.

    int addDigits(int num) {
        if(num == 0) return 0;
        int answer[] = {9,1,2,3,4,5,6,7,8};
        return answer[num%9];
    }
    
    /*
    01 02 03 04 05 06 07 08 09
    
      1--- -----------9
       9 1 2 3 4 5 6 7 8 
    10 - 19
    1 2 3 4 5 6 7 8 9 1
    20-29
    2 3 4 5 6 7 8 9 1 2
    30-39
    3 4 5 6 7 8 9 1 2 3
    40-49
    4 5 6 7 8 9 1 2 3 4
    
    100 101 102
    1    2   3
    110 111
    2    3
    */
    
    

  • 0
    L
    This post is deleted!

Log in to reply
 

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