Count Numbers with Unique Digits

来源:互联网 发布:网络星期一 2015 编辑:程序博客网 时间:2024/05/18 22:18

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])

https://www.hrwhisper.me/leetcode-count-numbers-unique-digits/

class Solution {public:    int countNumbersWithUniqueDigits(int n) {        if(n == 0)        {            return 1;        }        vector<int> current(n, 9);        current[0]=10;        for(int i = 1; i< n; i++)        {            for(int j = 9; j >= 11-i-1; j--)            {                current[i] *= j;            }        }        int sum =0;        for(int i = 0;i<n;i++)        {            sum += current[i];        }        return sum;    }};
0 0