Count Numbers with Unique Digits -- LeetCode
Given a non-negative integer n, count all numbers with unique digits, x, where 0 ≤ x < 10^n.
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])
思路:DP
dp[i]表示10^i以内的结果数。则10^0 = 1,则dp[0] = 0(只有0一个数),dp[1]=dp[0] + 所有非零的个位数=10.
则dp[i] = dp[i-1] + i位数中的结果数。
求i位数中的结果数很简单。假设我们要求所有3位数中满足要求的数,则最高位只可能是1到9这9个数,因为每一位不能重复,则次高位只能用0到9中没被用过的数共9个,第三位只能用剩下没被用过的0到9中的8个数,因此总数是9 * 9 * 8。 对于i来说,是9 * 9 *...*(11 - i).
class Solution {
public:
int countNumbersWithUniqueDigits(int n) {
if (n == ) return ;
int res = ;
for (int i = ; i <= std::min(, n); i++) {
int temp = ;
for (int j = ; j >= - i; j--)
temp *= j;
res += temp;
}
return res;
}
};
Count Numbers with Unique Digits -- LeetCode的更多相关文章
- 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] 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: 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
题目描述: 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. ...
- [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 ...
随机推荐
- 【志银】#define lowbit(x) ((x)&(-x))原理详解
分析下列语句 #define lowbit(x) ((x)&(-x)) 可写成下列形式: int Lowbit(x) { return x&(-x); } 例1:x = 1 十进制转二 ...
- python中os.path.join和join的区别
这两个函数都是python的系统函数,都有“组合”.“连接”之意,但用法和应用场景千差万别 函数说明: 1.join函数 用法:用于连接字符串数组.将字符串.元组.列表中的元素以指定的字符(即分隔符) ...
- POJ 1375 Intervals | 解析几何
参考了这个博客 #include<cstdio> #include<algorithm> #include<cstring> #include<cmath&g ...
- Dom的深度优先遍历和广度优先遍历
//深度优先遍历的递归写法 function DFTraversal(node) { var nodes = []; if (node != null) { nodes.push(node); var ...
- 块元素包裹img空隙问题
解决方法 1.img设为block2.img对齐方式vertical-align:bottom(middle/top),不是baseline就行3.父容器font-size:0或line-height ...
- 《c程序设计语言》读书笔记-递归实现快速排序算法
#include <stdio.h> void swap(int v[],int i,int j) { int temp; temp = v[i]; v[i] = v[j]; v[j] = ...
- Hibernate中inverse="true"的理解
Hibernate中inverse="true"的理解 举例如下 转自:http://lijiejava.iteye.com/blog/776587 Customer类: publ ...
- nutch 2.1安装问题集锦
参照官方文档http://nlp.solutions.asia/?p=180 中间碰到的问题,解决方法参考 http://blog.javachen.com/2014/05/20/nutch-intr ...
- webservice跨域文件,好多年前的东西,远程调用,js服务器端使用,可以远程调用
1.clientaccesspolicy.xml <?xml version="1.0" encoding="utf-8" ?> <acces ...
- 算法详解(LCA&RMQ&tarjan)补坑啦!完结撒花(。◕ˇ∀ˇ◕)
首先,众所周知,求LCA共有3种算法(树剖就不说了,太高级,以后再学..). 1.树上倍增(ST表优化) 2.RMQ&时间戳(ST表优化) 3.tarjan(离线算法)不讲..(后面补坑啦!) ...