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

  1. 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】357. Count Numbers with Unique Digits

    题目描述: Given a non-negative integer n, count all numbers with unique digits, x, where 0 ≤ x < 10n. ...

  3. 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. ...

  4. 【LeetCode】357. Count Numbers with Unique Digits 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  5. 357 Count Numbers with Unique Digits 计算各个位数不同的数字个数

    给定一个非负整数 n,计算各位数字都不同的数字 x 的个数,其中 0 ≤ x < 10n.示例:给定 n = 2,返回 91.(答案应该是除[11,22,33,44,55,66,77,88,99 ...

  6. [LeetCode] Count Numbers with Unique Digits 计算各位不相同的数字个数

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

  7. Count Numbers with Unique Digits

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

  8. Leetcode: Count Numbers with Unique Digits

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

  9. [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 ...

随机推荐

  1. Yii2 DatePicker时间选择器的使用

    bootstrap-Date Time Picker 详细配置请参考http://www.bootcss.com/p/bootstrap-datetimepicker/ use kartik\widg ...

  2. 如何在GitBook中使用Git管理

    1.在GitBook中新建[Book],找到对应目录下的位置,拷贝[book]名称后删除文件夹以及文件,新建文件夹重命名为[book]名称 2.打开文件夹,在根目录下选择创建git仓库,即 git i ...

  3. shutdown immediate 持久无法关闭数据库之解决方案

    问题引出:测试环境,进行oralce的shutdown immediate,等待时间很长,长的无法等待 ORACLE shutdown 过程: 1.shutdown normal(正常关闭方式):阻止 ...

  4. uva 1440 & uvalive 4597

    题目链接 题意: DAG的最小路径覆盖,一条边可以被重复覆盖多次,但是一次只能沿着DAG的方向覆盖一条链,问最少覆盖次数. 思路: 看了半天没有思路,所以去搜索了题解,然后发现是有源汇上下界的最小流, ...

  5. 网络协议相关面试问题-TLS与SSL握手

    HTTPS是什么? HTTPS并不是一个单独的协议,而是对工作在一加密连接(SSL / TLS)上的常规HTTP协议.通过在TCP和HTTP之间加入TLS(Transport Layer Securi ...

  6. TOMCAT控制台日志输出到指定文件中

    1 .修改startup.bat第42行 call "%EXECUTABLE%" start %CMD_LINE_ARGS% 为 call "%EXECUTABLE%&q ...

  7. python+request+HTMLTestRunner+unittest接口自动化测试框架

    转自https://my.oschina.net/u/3041656/blog/820023 正在调研使用python进行自动化测试,在网上发现一篇比较好的博文,作者使用的是python3,但目前自己 ...

  8. python在pycharm中导包一直出错的问题

    之前的net在code的子目录中,怎么调试都无法解决.最后用一个简单粗暴的方式,将net直接拿到项目的根目录中.如此即可

  9. New!Devexpress WinForms各版本支持Visual Studo和SQL Server版本对应图

    点击获取DevExpress v19.2.3最新完整版试用下载 本文主要为大家介绍DevExpress WinForms各大版本支持的VS版本和支持的.Net版本图,Devexpress WinFor ...

  10. 喜大普奔!.NET界面控件DevExpress v19.2发布,快来下载体验

    DevExpress Universal Subscription(又名DevExpress宇宙版或DXperience Universal Suite)是全球使用广泛的.NET用户界面控件套包,De ...