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. SQL练习汇总

    --1.选择部门30中的所有员工. --2.列出所有办事员(CLERK)的姓名,编号和部门编号. select ename,empno,deptno from emp where job='CLERK ...

  2. 十五,K8S集群调度原理及调度策略

    目录 k8s调度器Scheduler Scheduler工作原理 请求及Scheduler调度步骤: k8s的调用工作方式 常用预选策略 常用优先函数 节点亲和性调度 节点硬亲和性 节点软亲和性 Po ...

  3. Git学习笔记(2)-Eclipse中Git插件使用

    目前我使用的Eclipse luna版本中已经集成了git插件,这里就不介绍如何安装Git插件了,不懂可以看其他的博客. 上篇笔记介绍了Git的基本指令,实际开发中我基本都使用eclipse插件进行代 ...

  4. [工具] fierce--子域收集

    简介 fierce 是使用多种技术来扫描目标主机IP地址和主机名的一个DNS服务器枚举工具.运用递归的方式来工作.它的工作原理是先通过查询本地DNS服务器来查找目标DNS服务器,然后使用目标DNS服务 ...

  5. 【AGC002 E】Candy Piles

    本来实在写不动这题 sol 了,但一想这是个经典的模型转化问题,于是就写了(.jpg) 题意 有一个序列 \(a_i\). 两人轮流操作,每次操作为二选一: 1. 把最大的 \(a_i\) 减成 \( ...

  6. BZOJ 1006 完美消除序列&最大势算法&弦图

    K国是一个热衷三角形的国度,连人的交往也只喜欢三角原则.他们认为三角关系:即AB相互认识,BC相互认识,CA相互认识,是简洁高效的.为了巩固三角关系,K国禁止四边关系,五边关系等等的存在.所谓N边关系 ...

  7. 第十五届四川省省赛 SCU - 4439 Vertex Cover

    给你一个一般图 保证每条边的一端下标不大于30 问最小覆盖集的大小为多少 爆搜:枚举前30个点是否在覆盖集内 剪枝1:如果不在的话 那么他所连的下标大于30的点都必须选 剪纸2:最优解剪枝 #incl ...

  8. Springboot + Mybatis + Ehcache

    最近在做一个项目,为处理并发性较差的问题,使用了Mybatis二级缓存 但在多表联合查询的情况下,Mybatis二级缓存是存在着数据脏读的问题的 两天就是在想办法解决这个数据脏读的问题 考虑到简易性. ...

  9. python+Appium自动化:运行第一个appium脚本

    用pycharm工具进行编写,运行一个开启淘宝app实例 from appium import webdriver desired_caps = { "platformName": ...

  10. 锁&lock与latch

    锁是数据库系统区别于文件系统的一个关键特性.锁机制用于管理对共享资源的并发访问.Innodb不仅仅使用行锁,也会在数据库内部其它地方使用锁,从而允许对多种不同资源提供并发访问.如:操作缓冲池中的LRU ...