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

Analysis:

A number of unique digits is a number which is a combination of unrepeated digits. So, we can calculate the total number. for number with n digits, like 100-999 or 1000-9999, the total numbers with unique digits equals to 9*9*8...*(11-n). because the highest digit cannot be 0.

Here is DP. dp[i] is the count of all i-digit numbers with unique digits, dp[i] = dp[i-1]*(11-i) for i from 2 to n

 public static int countNumbersWithUniqueDigits(int n) {
if (n == 0) {
return 1;
}
int ans = 10, base = 9;
for (int i = 2; i <= n && i <= 10; i++) {
base = base * (11 - i);
ans += base;
}
return ans;
}

第一遍backtracking做法:

 public class Solution {
public int countNumbersWithUniqueDigits(int n) {
if (n == 0) return 1;
if (n == 1) return 10;
int res = 10;
for (int i=2; i<=n && i<=10; i++) {
int count = 1;
int bit = 9;
for (int j=0; j<i; j++) {
count *= bit;
if (j != 0) bit--;
}
res += count;
}
return res;
}
}

Leetcode: Count Numbers with Unique Digits的更多相关文章

  1. [LeetCode] Count Numbers with Unique Digits 计算各位不相同的数字个数

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

  2. LC 357. Count Numbers with Unique Digits

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

  3. 【LeetCode】357. Count Numbers with Unique Digits 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  4. 【Leetcode】357. Count Numbers with Unique Digits

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

  5. Java [Leetcode 357]Count Numbers with Unique Digits

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

  6. Count Numbers with Unique Digits -- LeetCode

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

  7. [Swift]LeetCode357. 计算各个位数不同的数字个数 | Count Numbers with Unique Digits

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

  8. Count Numbers with Unique Digits

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

  9. 357. Count Numbers with Unique Digits

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

随机推荐

  1. 云计算中心网络资源分配-Faircloud: sharing the network in cloud computing

    网络资源同计算资源以及存储资源一样,是一种可被租户共享使用并提高利用率的资源.但是,不同租户的计算资源以及存储资源之间,有很强的隔离性,可以实现按需按比例分配的使用方式,但是网络资源却不可以. 主要原 ...

  2. Solr高亮详解

    hl.fl: 用空格或逗号隔开的字段列表.要启用某个字段的highlight功能,就得保证该字段在schema中是stored.如果该参数未被给出,那么就会高亮默认字段 standard handle ...

  3. Linux shell 变量 数学 运算

    Abstract : 1)  Linux shell 中使用 let , [ ] ,(( )) 三种运算符操作 shell 变量进行简单的基本运算: 2)Linux shell 中使用 expr 与 ...

  4. final与static

    一.final 根据程序上下文环境,Java关键字final有“这是无法改变的”或者“终态的”含义,它可以修饰非抽象类.非抽象类成员方法和变量.你可能出于两种理解而需要阻止改变:设计或效率. fina ...

  5. arch框架人员、组织说明

    目前ERP辅助系统集成了三大模块功能,分别是财务辅助.物理辅助.报账平台. 财务辅助模块人员在ARCH_USER 表中进行管理,通过单独的[用户映射功能]将ARCH系统用户和ERP用户进行关联,关联信 ...

  6. html之内联元素与块状元素;

    html之内联元素与块状元素 一.html之内联元素与块状元素 1.块状元素一般比较霸道,它排斥与其他元素位于同一行内.比如div,并且width与height对它起作用. 2.内联元素只能容纳文本或 ...

  7. cvWaitKey 如果 cvNamedWindow就不会起作用

    Have you called cvNamedWindow yet? It will not work without cvNamedWindow. http://stackoverflow.com/ ...

  8. global.asax、global.asax.compiled、PrecompiledApp.config三者关系

    global.asax用WebDeploy发布后,会在bin下面产生一个global.asax.compiled,同时根目录下产生PrecompiledApp.config. 正常情况下global. ...

  9. Charles辅助调试接口

    http://blog.sina.com.cn/s/blog_6ae8b50d0102w7tw.html

  10. LightOj 1138 - Trailing Zeroes (III) 阶乘末尾0的个数 & 二分

    题目链接:http://lightoj.com/volume_showproblem.php?problem=1138 题意:给你一个数n,然后找个一个最小的数x,使得x!的末尾有n个0:如果没有输出 ...