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的更多相关文章

  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] 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: Count Numbers with Unique Digits

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

  5. 【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. 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. ...

  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. virsh command

    http://www.cnblogs.com/chenjiahe/category/845519.html resize qemu-img resize win2k8r2.qcow2 40G conv ...

  2. 团队Alpha(八)冲刺

    目录 组员情况 组员1(组长):胡绪佩 组员2:胡青元 组员3:庄卉 组员4:家灿 组员5:凯琳 组员6:翟丹丹 组员7:何家伟 组员8:政演 组员9:黄鸿杰 组员10:刘一好 组员11:何宇恒 展示 ...

  3. ActiveX 控件和 Web 浏览器加载项

    百度ActiveX的概念. 如何从零开始写一个 Chrome 扩展 360极速浏览器应用开发平台.

  4. poj 1840 枚举

    Eqs Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 13967   Accepted: 6858 Description ...

  5. select chosen 禁用下拉框某一个option

    $("#tbParBudCode option[value='" + budCodeId + "']").attr("disabled", ...

  6. 【bzoj1486】[HNOI2009]最小圈 分数规划+Spfa

    题目描述 样例输入 4 5 1 2 5 2 3 5 3 1 5 2 4 3 4 1 3 样例输出 3.66666667 题解 分数规划+Spfa判负环 二分答案mid,并将所有边权减去mid,然后再判 ...

  7. 个人收藏的移动端网页布局rem解决方案

    写移动端项目时,总是会纠结是用css3 media query 还是用rem.移动端框架挺多,但是因为项目都比较小,不考虑使用. 无意在网上找到一个移动端rem布局的解决方案,经个人实践,目前未出现什 ...

  8. [THUWC2017][bzoj5020] 在美妙的数学王国中畅游 [LCT+泰勒展开]

    题面 LOJ传送门 思路 这里很重要 它提示我们,把给定的三个函数泰勒展开,并用LCT维护每一项泰勒展开式的值,维护十几项就满足了题目的精度要求 我们考虑一个函数在0位置的泰勒展开 $f(x)=\su ...

  9. BZOJ 3052: [wc2013]糖果公园 | 树上莫队

    题目: UOJ也能评测 题解 请看代码 #include<cstdio> #include<algorithm> #include<cstring> #includ ...

  10. BZOJ2875 [Noi2012]随机数生成器 【矩阵乘法 + 快速乘】

    题目 栋栋最近迷上了随机算法,而随机数是生成随机算法的基础.栋栋准备使用线性同余法(Linear Congruential Me thod)来生成一个随机数列,这种方法需要设置四个非负整数参数m,a, ...