[动态规划] leetcode 357 Count Numbers with Unique Digits

problem:https://leetcode.com/problems/count-numbers-with-unique-digits/

1 : 10

2: 10 + 9 x 9

3: 10 + 9 x 9 + 9 x 9 x 8

4: 10 + 9 x 9 + 9 x 9 x 8 x 7

……

class Solution {
public:
    int countNumbersWithUniqueDigits(int n) {
        if(n == 0) return 1;
        int llast = 1;
        int last = 10;
        for(int i = 2;i <= n;i++)
        {
            int result = last + (last - llast) * (10 - (i - 1));
            llast = last;
            last = result;
        }
        return last;
    }
};

猜你喜欢

转载自www.cnblogs.com/fish1996/p/11332637.html