首页 > 其他 > 详细

357. Count Numbers with Unique Digits

时间:2019-11-16 11:36:59      阅读:67      评论:0      收藏:0      [点我收藏+]

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

Example:

Input: 2
Output: 91 
Explanation: The answer should be the total numbers in the range of 0 ≤ x < 100, 
             excluding 11,22,33,44,55,66,77,88,99

class Solution {
    public int countNumbersWithUniqueDigits(int n) {
        if (n == 0) {
            return 1;
        } 
        int ret = 10, count = 9;
        for (int i = 2; i <= n; i++) {
            count *= 9-i+2;
            ret += count;
        }
        return ret;
    }
}

当n=1时因为只有一个数字,所以0-9都是答案.当n>=2时,最高位可以为1-9任意一个数字,之后各位可以选择的数字个数依次为9, 8, 7, 6...,上一位选一个下一位就少了一种选择.

https://blog.csdn.net/qq508618087/article/details/51656771

357. Count Numbers with Unique Digits

原文:https://www.cnblogs.com/wentiliangkaihua/p/11870954.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!