Leetcode: Count Numbers with Unique Digits
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的更多相关文章
- [LeetCode] Count Numbers with Unique Digits 计算各位不相同的数字个数
Given a non-negative integer n, count all numbers with unique digits, x, where 0 ≤ x < 10n. Examp ...
- 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 ...
- 【LeetCode】357. Count Numbers with Unique Digits 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【Leetcode】357. Count Numbers with Unique Digits
题目描述: Given a non-negative integer n, count all numbers with unique digits, x, where 0 ≤ x < 10n. ...
- 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. ...
- 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 ...
- [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 ...
- Count Numbers with Unique Digits
Given a non-negative integer n, count all numbers with unique digits, x, where 0 ≤ x < 10n. Examp ...
- 357. Count Numbers with Unique Digits
Given a non-negative integer n, count all numbers with unique digits, x, where 0 ≤ x < 10n. Examp ...
随机推荐
- buffer overflow
Computer Systems A Programmer's Perspective Second Edition We have seen that C does not perform any ...
- 拼写纠正 Artificial Intelligence: A Modern Approach
Artificial Intelligence: A Modern Approach http://mindhacks.cn/2008/09/21/the-magical-bayesian-metho ...
- 如何去掉delphi2010的欢迎界面(welcome page)
如何去掉delphi2010的欢迎界面(welcome page)方法一: 在电脑开始菜单下,找到delphi的快捷菜单,点击该菜单的属性,在“目标”的内容中,最后添加“-np”即可.如:D:\Win ...
- NRF51822之app_button使用
我们现在开始使用app_button,为什么要使用这个来替代直接使用GPIOTE呢? 因为我们在手册中可以看到如果一直开启GPIOTE in模式的需要需要很大电流.所以我们需要使用RTC来“周期”的查 ...
- 解读ClassLoader
ClassLoader一个经常出现又让很多人望而却步的词,本文将试图以最浅显易懂的方式来讲解 ClassLoader,希望能对不了解该机制的朋友起到一点点作用. 要深入了解ClassLoader ...
- Mongo中的数组操作
当前mongo中有这么一条数据 book是一个数组,在他后面添加一条数据 { "_id" : ObjectId("5721f504d1f70435632b5ce7&quo ...
- sqlserver快速查找所有存储过程中是否包含某字符
--将text替换成你要查找的内容 select name from sysobjects o, syscomments s where o.id = s.id and text like '%tex ...
- Bundle文件的创建和使用(二)
1.概念: An NSBundle object represents a location in the file system that groups code and resources tha ...
- 用javascript实现用户登录验证
<script language=javascript> function checkSubmit() { if ((document.form1.name.value)=="& ...
- JavaScipt选取文档元素的方法
摘自JavaScript权威指南(jQuery根据样式选择器查找元素的终极方式是 先用getElementsByTagName(*)获取所有DOM元素,然后根据样式选择器对所有DOM元素进行筛选) 选 ...