LC 357. Count Numbers with Unique Digits
Given a non-negative integer n, count all numbers with unique digits, x, where 0 ≤ x < 10n.
Example:
Input: 2
Output: 91
Explanation: The answer should be the total numbers in the range of 0 ≤ x < 100,
excluding11,22,33,44,55,66,77,88,99
Runtime: 0 ms, faster than 100.00% of C++ online submissions for Count Numbers with Unique Digits.
额有点蠢。
f(k) = 9 * 9 * 8 * ... * (9 - i + 2) 第一位是9 因为 0 不能在第一位。
class Solution {
public:
int countNumbersWithUniqueDigits(int n) {
if(n == ) return ;
vector<int> dp(n+, );
dp[] = ;
for(int i=; i<n+; i++){
dp[i] = dp[i-] * (-i+);
}
int ret = ;
for(int i=; i<n+; i++) ret += dp[i];
ret += ;
return ret;
}
};
DFS
Runtime: 116 ms, faster than 2.80% of C++ online submissions for Count Numbers with Unique Digits.
class Solution {
public:
int countNumbersWithUniqueDigits(int n) {
int maxval = pow(,n), ret = , used = ;
for(int i=; i<; i++){
used |= (<<i);
search(i, maxval, ret, used);
used &= ~(<<i);
}
return ret;
}
void search(int prev, int maxval, int& ret, int& used){
if(prev < maxval) ret++;
else return ;
for(int i=; i<; i++){
if(!(used & ( << i))){
used |= (<<i);
search(*prev+i, maxval, ret, used);
used &= ~(<<i);
}
}
}
};
LC 357. Count Numbers with Unique Digits的更多相关文章
- 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
题目描述: 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. ...
- 【LeetCode】357. Count Numbers with Unique Digits 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 357 Count Numbers with Unique Digits 计算各个位数不同的数字个数
给定一个非负整数 n,计算各位数字都不同的数字 x 的个数,其中 0 ≤ x < 10n.示例:给定 n = 2,返回 91.(答案应该是除[11,22,33,44,55,66,77,88,99 ...
- [LeetCode] 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 ...
- Leetcode: Count Numbers with Unique Digits
Given a non-negative integer n, count all numbers with unique digits, x, where 0 ≤ x < 10n. Examp ...
- [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 ...
随机推荐
- export命令用来设置环境变量。
sh-# export export CONSOLE="/dev/console" export HOME="/" export LD_LIBRARY_PATH ...
- Delphi MSCom安装
- visio的形状默认是蓝色的填充色,怎么设置为白色为默认色?
如图所示: 设计->主题->选黑白那个 效果如下:
- TensorFlow可以在终端和通过终端打开的PyCharm中运行,不能在直接打开的PyCharm中运行
然后看运行窗口的出错信息,点击最右边的view,发现缺少个文件,如代码所示 Traceback (most recent call last): File "/usr/local/lib/p ...
- BZOJ 1008 组合数学
监狱有连续编号为1...N的N个房间,每个房间关押一个犯人,有M种宗教,每个犯人可能信仰其中一种.如果相邻房间的犯人的宗教相同,就可能发生越狱,求有多少种状态可能发生越狱 总的情况为mn不越狱的情况为 ...
- 配置LANMP环境(6)-- 安装APACHE与PHP配置
一.安装 Apache 2.4 安装:默认安装2.4版本 yum install httpd 修改配置 vim /etc/httpd/conf/httpd.conf 42行80端口改为 8080查看行 ...
- crc32 cpp Makefile可参考
https://github.com/stbrumme/crc32 # simple Makefile CPP = g++ # files PROGRAM = Crc32Test LIBS = -lr ...
- TCP_Wrappers应用层防火墙
TCP_Wrappers是一个工作在应用层的安全工具,它只能针对某些具体的应用或者服务起到一定的防护作用.比如说ssh.telnet.FTP等服务的请求,都会先受到TCP_Wrappers的拦截. T ...
- 【构造 meet in middle 随机 矩阵树定理】#75. 【UR #6】智商锁
没智商了 变式可见:[构造 思维题]7.12道路建设 当你自信满满地把你认为的正确密码输入后,时光机滴滴报警 —— 密码错误.你摊坐在了地上. 黑衣人满意地拍了拍你的肩膀:“小伙子,不错嘛.虽然没解开 ...
- 内置对象-Math
1.随机数 Math.random() 1)获得0-1之间的随机数 2)0到100:Math.round(Math.random()*100) 2.max:求最大值 ,,,) console.log( ...