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. GraphX介绍

    转自:https://www.cnblogs.com/txq157/p/5978747.html 1.GraphX介绍 1.1 GraphX应用背景 Spark GraphX是一个分布式图处理框架,它 ...

  2. yocto project user’s guide

    http://www.yoctoproject.org/docs/2.1/ref-manual/ref-manual.html 参考手册 http://www.yoctoproject.org/doc ...

  3. deep_learning_Dropout

    吴恩达深度学习笔记(十一)—— dropout正则化 主要内容: 一.dropout正则化的思想 二.dropout算法流程 三.dropout的优缺点 一.dropout正则化的思想 在神经网络中, ...

  4. BLE 5协议栈-通用属性规范层(GATT)

    文章转载自:http://www.sunyouqun.com/2017/04/page/2/ 通用属性规范GATT(Generic Attribute Profile)将ATT层定义的属性打包成不同的 ...

  5. 编译TensorFlow-serving GPU版本

    编译TensorFlow-serving GPU版本 TensorFlow Serving 介绍 编译GPU版本 下载源码 git clone https://github.com/tensorflo ...

  6. .NET 树型递归

    /// <summary> /// 获取全部水价标准模型 /// </summary> /// <returns></returns> public I ...

  7. MyBatis-04-配置解析

    4.配置解析 1.核心配置文件 mybatis-config.xml MyBatis的配置文件包含了会深深影响MyBatis行为的设置和属性信息 configuration(配置) propertie ...

  8. mybatic进阶遗留

    参考文章: MyBatis的架构设计以及实例分析 MyBatis缓存机制的设计与实现 MyBatis的一级缓存实现详解 及使用注意事项 MyBatis的二级缓存的设计原理

  9. Django2.1.1与xadmin0.6.0遇到的坑

    Django2.1.1与xadmin0.6.0遇到的坑 BlueMiaomiao关注4人评论11188人阅读2018-09-23 12:17:56 (1)django2.0把from django.c ...

  10. BZOJ 3626 [LNOI2014]LCA 树剖+(离线+线段树 // 在线+主席树)

    BZOJ 4012 [HNOI2015]开店 的弱化版,离线了,而且没有边权(长度). 两种做法 1 树剖+离线+线段树 这道题求的是一个点zzz与[l,r][l,r][l,r]内所有点的lcalca ...