[LeetCode] 357. Count Numbers with Unique Digits

来源:互联网 发布:2017最新拼图软件 编辑:程序博客网 时间:2024/06/01 18:16

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

// 0msclass Solution {public:    int countNumbersWithUniqueDigits(int n) {        int total = 0, ncomb = 1;        n = min(n, 10);        for (int i = 0; i < n; i++) {            ncomb *= (10 - i);            total += ncomb;            if (i == 0) ncomb--;        }        return total == 0 ? 1 : total;    }};
原创粉丝点击