357. Count Numbers with Unique Digits 数学 找规律

来源:互联网 发布:不要网络的单机游戏 编辑:程序博客网 时间:2024/06/09 21:47

Given a non-negative integer n, count all numbers with unique digits, x, where 0 ≤ x < 10n.

Example:
Given n = 2, return 91. (The answer should be the total numbers in the range of 0 ≤ x < 100, excluding [11,22,33,44,55,66,77,88,99])

Credits:
Special thanks to @memoryless for adding this problem and creating all test cases.

class Solution {public:    int countNumbersWithUniqueDigits(int n) {        if(n==0) return 1;        if(n==1) return 10;        int res=10;        int temp=81;        int i=8;        while(n>1)        {            res+=temp;            temp*=i--;            n--;        }        return res;    }};