How can this solution be improved to make it faster?


  • 0
    S

    I use a recursion algorithm to solve this question,it works right,but the result shows it only faster than 15% C++ solution,then i wonder how can modify these codes to make it faster.Thanks!

    class Solution {
    public:
        int countNumbersWithUniqueDigits(int n) {
            if (n == 0){
                return 1;
            }
            if (n == 1){
                return 10;
            }
            int temp = 9;
            int power = 9;
            for(int i=0;i<n-1;++i){ //n=3时,3次
                temp *= power;
                power -= 1;
            }
            return countNumbersWithUniqueDigits(n - 1) + temp;
        }
    };
    

Log in to reply
 

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