题目描述:

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

解题思路:

  1. This problem can also be solved using a dynamic programming approach and some knowledge of combinatorics.
  2. Let f(k) = count of numbers with unique digits with length equals k.
  3. f(1) = 10, ..., f(k) = 9 * 9 * 8 * ... (9 - k + 2) [The first factor is 9 because a number cannot start with 0].

代码如下:

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

  

Java [Leetcode 357]Count Numbers with Unique Digits的更多相关文章

  1. 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 ...

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

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

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

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

  4. 357. Count Numbers with Unique Digits

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

  5. 357 Count Numbers with Unique Digits 计算各个位数不同的数字个数

    给定一个非负整数 n,计算各位数字都不同的数字 x 的个数,其中 0 ≤ x < 10n.示例:给定 n = 2,返回 91.(答案应该是除[11,22,33,44,55,66,77,88,99 ...

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

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

  7. Leetcode: 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 -- LeetCode

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

  9. [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 ...

随机推荐

  1. linux java环境配置

    一.安装 创建安装目录,在/usr/java下建立安装路径,并将文件考到该路径下: # mkdir /usr/java 1.jdk-6u11-linux-i586.bin 这个是自解压的文件,在lin ...

  2. ASP.NET MVC Select无限级分类选择下拉框

    1:读取父级下的所有子类别 *ViewBag.ParentItemList:不能与ParentId相同 private void ParentDropDownList() { List<SAS. ...

  3. jQuery上下切换带缩略图的焦点图

    在线演示 本地下载

  4. Oracle 集合操作

    在 Oracle 中提供了三种类型集合操作:并(UNION).交(INTERSECT).差(MINUS) · UNION:将多个查询的结果组合到一个查询结果之中,没有重复内容 · UNION ALL: ...

  5. Spring_管理 Bean 的生命周期

    beans-cycle.xml <?xml version="1.0" encoding="UTF-8"?><beans xmlns=&quo ...

  6. java基础(2)--进制

    进制 进制基础, 目的:理解计算机只能处理2进制的数据和指令 1)10进制计数规律 数字: 0 1 2 3 4 5 6 7 8 9 基数:10 权:  1000 100 10 1 权是基数的n次幂 2 ...

  7. Pow,求x的y次幂

    算法分析:很显然用递归.但是直接用递归会造成栈溢出,时间复杂度是o(n).所以要用分治思想,时间复杂度是o(logN). public class Power { //栈溢出,时间复杂度是o(n) p ...

  8. thinkphp3.2.3 定时任务重新加载, 无法加载新的定时任务的问题

    thinkphp3.2.3 的定时任务有个坑,一旦你改名定时任何或者路径,新的定时任务将无法加载,无论你重启php还是重启nginx,甚至重启服务器,都不行. 原因是你要删掉一个类似lock文件,才可 ...

  9. 关于jQuery中的offset()和position()

    在jQuery中有两个获取元素位置的方法offset()和position().position()方法是在1.2.6版本之后加入的,为什么要引 入这个方法呢?这两个方法之间有什么异同?使用的时候应该 ...

  10. StringUtils在commons-lang3和commons-lang中的区别【转】

    http://blog.csdn.net/eden_m516/article/details/75042439 最近经常需要对String做一些判断和处理,于是就用到了Apache提供的StringU ...