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 ...
随机推荐
- 上班时能不能戴耳机?V
上班时能不能戴耳机? 新入职一公司, 上班时间不能戴耳机在V站一石激起千层浪,网友意见主要分几派: 甩手走人型: 神经病公司,这还不赶紧走 不走等着过年 不走留着转正 离职,下一题 还是赶紧离职吧 这 ...
- KeyError: 'pass_ticket'
使用wxpy模块对接微信登陆,在扫描程序弹出二维码后,使用手机微信扫描登陆之后报KeyError: 'pass_ticket'. 原因就是微信禁止该账号登陆微信网页版,如下图: 所以,去找到能让账号登 ...
- Redis03——Redis架构
Redis架构 1.1.问题 redis是单线程,单实例,为什么并发那么多,依旧很快呢? 回答:因为调用了系统内核的epoll 1.2.Linux的早期版本 Linux有Linux kernal,我们 ...
- Java语言基础(12)
1 构造方法重载 在一个类内部,编写多个构造方法,创建对象的时候,根据需求的不同,调用不同的构造方法创建对象,实现不同的初始化. 案例:Demo1 public class Demo1 { publi ...
- js抽奖,跑马灯
分享自己写的跑马灯抽奖. HTML代码 <!--首先将一个div的背景设为一个圆形--> <div style=" width:240px; height:232px; b ...
- CeSharp支持MP4
因为CefSharp不支持MP4格式(因为版权问题,MP3因为版权过期新版本已经支持了),需要自己下载源码重新编译以支持MP4,或者下载被人编译好的库.因时间问题,我直接在csdn上下载了一个(1c币 ...
- Token认证的优势与劣势
token 我们说的token,是指 访问资源的凭据 .使用基于 Token 的身份验证方法,在服务端不需要存储用户的登录记录. 大致流程: 客户端使用用户名跟密码请求登录服务端收到请求,验证用户名与 ...
- css3正方体
使用animation和调整页面角度做出来 <html lang="zh"> <head> <meta charset="UTF-8&quo ...
- CodeForces - 1204E Natasha, Sasha and the Prefix Sums (组合数学,卡特兰数扩展)
题意:求n个1,m个-1组成的所有序列中,最大前缀之和. 首先引出这样一个问题:使用n个左括号和m个右括号,组成的合法的括号匹配(每个右括号都有对应的左括号和它匹配)的数目是多少? 1.当n=m时,显 ...
- .Net界面开发神器—DevExpress官方汉化包免费下载!还在等什么?
点击获取DevExpress v19.1.7新版试用下载 DevExpress Localization Service允许您创建一组自定义的附属程序集,要将语言包添加到程序集中,请查看本文中为大家列 ...