C++ DP by appending digits to the end


  • 1
    C
    class Solution {
    public:
        int countNumbersWithUniqueDigits(int n) {   
            if(n == 0) return 1;
            int count = 9, ret = 9;
            // In each iteration, try adding 0 - 9 to the end of number
            for(int i = 2; i <= n; ++i) {
                // Advoid repeating digits. f(k) = f(k - 1) * (10 - k - 1)
                count *= 11 - i;
                ret += count;
            }
            // Add 0
            return ret + 1;
        }
    };

Log in to reply
 

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