leetcode:数学:Count Numbers with Unique Digits(357)

原创 2016年08月31日 14:19:06

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


http://www.cnblogs.com/grandyang/p/5582633.html


class Solution {
public:
    int countNumbersWithUniqueDigits(int n) {
        if (n == 0) return 1;
        int res = 0;
        for (int i = 1; i <= n; ++i) {
            res += count(i);
        }
        return res;
    }
    int count(int k) {
        if (k < 1) return 0;
        if (k == 1) return 10;
        int res = 1;
        for (int i = 9; i >= (11 - k); --i) {
            res *= i;
        }
        return res * 9;
    }
};
版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

LeetCode #357 - Count Numbers with Unique Digits - Medium

ProblemGiven a non-negative integer n, count all numbers with unique digits, x, where 0 ≤ x < 10n. H...
  • Arcome
  • Arcome
  • 2016年11月02日 08:10
  • 154

Leetcode 357. Count Numbers with Unique Digits 统计没有重复数字的数 解题报告

1 解题思想这道题其实是一个高中数学题(其实也是考研概率论的题目) 其做法也就是一个排列组合的关系,因为我们需要统计0~10^n里面不包含重复数字的所有数字,所以其实很简单: 对于位数为i的情况 ...
  • MebiuW
  • MebiuW
  • 2016年07月06日 20:01
  • 776

leetcode_357 Count Numbers with Unique Digits

题意分析:给定非负的整数n,求在0

LeetCode笔记:357. Count Numbers with Unique Digits

计算唯一组成数出现的数量

Leetcode-357- Count Numbers with Unique Digits C#

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

<LeetCode OJ> 357. Count Numbers with Unique Digits

Total Accepted: 450 Total Submissions: 1077 Difficulty: Medium Given a non-negative integer n, ...

LeetCode 357. Count Numbers with Unique Digits

357. Count Numbers with Unique DigitsGiven a non-negative integer n, count all numbers with unique d...

【算法作业15】LeetCode 357. Count Numbers with Unique Digits

第十五周算法作业:LeetCode 357. Count Numbers with Unique Digits

357. Count Numbers with Unique Digits

题目链接:https://leetcode.com/problems/count-numbers-with-unique-digits/ 题目描述: Given a non-negativ...

[DP]357. Count Numbers with Unique Digits

题目: Given a non-negative integer n, count all numbers with unique digits, x, where 0 ≤ x n. Exam...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:leetcode:数学:Count Numbers with Unique Digits(357)
举报原因:
原因补充:

(最多只允许输入30个字)