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. apache安装 mod_evasive

    centos环境下安装 首先安装httpd-devel yum安装mod_evasive 配置mod_evasive: DOSHashTableSize 3097      #哈希表大小(无需修改)  ...

  2. access violation at address General protection fault

    https://en.wikipedia.org/wiki/General_protection_fault In memory errors, the faulting program access ...

  3. url匹配和match()方法

    下面的全局匹配可以找到字符串中的所有数字: "1 plus 2 equals 3".match(/\d+/g) // 返回 ["1", "2" ...

  4. 智能手机,医疗诊断,云会议(gotomeeting/citrix)

    在诊断领域已出现很多大有希望的创新,它们可能会起到真正的变革作用. 例如,有一种新技术可以让健康护理工作者用一部智能手机拍摄高质量的视网膜图像.这些数码照片像素很高,足以帮助检测白内障.黄斑退化.糖尿 ...

  5. sqlserver 通过convert取得指定格式的时间

    http://msdn.microsoft.com/zh-cn/library/ms187928(v=sql.105).aspx CONVERT(NVARCHAR(10),Created,112) 不 ...

  6. Nodejs路由之间的数据传递

    实例是模拟登录页面提交表单,然后根据信息判断是否登录成功 login.js var express =require('express'); var router =express.Router(); ...

  7. 一本很不错的书----DOOM启示录

    强推,所有玩游戏的和做游戏的热爱游戏的都应该看看. 摘录了一些话. 盖茨不明白,为什么啊为什么,为什么一个麦斯奎特的小公司,居然能从他手下挖走迈克尔·亚伯拉什,而且仅仅凭借几个游戏就胜过了自己的软件帝 ...

  8. mongoDB安装学习

    一: 下载安装 上MongoDB官网 ,下载之后安装 安装好了之后在对应的安装目录下就会看到安装的文件 二:启动 微软徽标+R,输入cmd,首先找到“mongodb”的路径,然后运行mongod开启命 ...

  9. 热门WEB前端职业你只需要掌握这些

    在知名的互联网企业里工作是一件很美好的事情,有很多的工作机会,而且企业们通过高薪以及令人羡慕的福利来争夺最优秀的人才.但是如果你花了大量的时间在招聘网站上和公司的帖子上,你可能会注意到在网页设计这个工 ...

  10. js数组判断是否含有某一个元素

    arr.indexOf('a')如果有则返回的a的下标位置,否则返回false.