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

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/count-numbers-with-unique-digits
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

我的思路1:(暴力)

1、最直接的就是暴力解,但是超时。到7的时候就超时了

class Solution {
public:int countNumbersWithUniqueDigits(int n) {int len =pow(10,n);int count = 0;for(int i = 0; i < len; i++){if(check(i) == false) count++;}return len - count;}bool check(int n){  //如果有重复,返回falsevector<bool> vis(10, false);if(n == 0) return true;while(n != 0){int yushu = n % 10;if(vis[yushu] == false){vis[yushu] = true;}else return false;n /= 10;}return true;}
};

参考思路2:(dp)

1、dp[i]表示i位数字不同的数字数

class Solution {
public:int countNumbersWithUniqueDigits(int n) {if(n == 0) return 1;int dp[11];dp[0] = 1;int per = 1;for(int i = 1; i <= 10; i++){dp[i] = dp[i-1] + 9*per;per = per * (10-i);}if(n <= 10) return dp[n];else return dp[10];}};

leetcode -- 357. Count Numbers with Unique Digits相关推荐

  1. LeetCode 357. Count Numbers with Unique Digits

    357. Count Numbers with Unique Digits Given a non-negative integer n, count all numbers with unique ...

  2. leetcode 357. Count Numbers with Unique Digits | 357. 计算各个位数不同的数字个数(Java)

    题目 https://leetcode.com/problems/count-numbers-with-unique-digits/ 题解 只要认真分析数字特点,这题不难,就是个带一些条件的排列组合. ...

  3. 357. Count Numbers with Unique Digits

    357. Count Numbers with Unique Digits Given a non-negative integer n, count all numbers with unique ...

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

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

  5. LeetCode Count Numbers with Unique Digits(计数问题)

    题意:给出一个整数,求在[0,10^n]之间各位数字不相同的数的个数 思路:n为1时,有10个 n为2时, 十位数只能是1-9,而个数位可以为0,所以也是有9种选择 n为3时,百位数有9种选择,十位数 ...

  6. mysql unique count_MySQL - Count Number of Unique Values

    问题 If I have three columns: orderNumber, name, email and I would like to count how many unique email ...

  7. pandas使用nunique函数计算dataframe每个数据列的独特值的个数(count number of unique values in each column of dataframe)

    pandas使用nunique函数计算dataframe每个数据列的独特值的个数(count number of unique values in each column of dataframe) ...

  8. LeetCode 829. Consecutive Numbers Sum--笔试题--C++解法

    LeetCode 829. Consecutive Numbers Sum–笔试题–C++解法 LeetCode题解专栏:LeetCode题解 LeetCode 所有题目总结:LeetCode 所有题 ...

  9. LeetCode 38. Count and Say

    问题链接 LeetCode 38. Count and Say 题目解析 找规律,每一个数字串是上一个数字串的"读法".比如:n=1时为"1",读作" ...

最新文章

  1. android学习笔记34——ClipDrawable资源
  2. Concert Tour(Uvalive 6853)
  3. 汇编语言---键盘KeyCode值列表
  4. msgpack pack php,msgpack_pack和redis的诡异事
  5. Ionic Mac 环境配置
  6. centos7.4编译mysql5.6,centos7编译安装mysql5.6
  7. 操作系统——文件的逻辑结构
  8. php面向对象编程代码怎么写,php面向对象编程(一)
  9. labview的介绍,安装和卸载
  10. 一个C++工程CPU占用100%问题的排查
  11. 2020 年微服务领域开源数字化报告发布,阿里扛起微服务大旗
  12. 获取CPUID序列号的两种办法
  13. 安居客头部买房部分书写
  14. 南方cass10.1中文版
  15. 神经网络前景背景分析,人工神经网络研究背景
  16. Windows 10搭建SVN服务器
  17. 一些触动人心的动效设计欣赏
  18. 沪深股票接口如何获取所有股票代码?
  19. cad抠图 lisp_用Autolisp对AutoCAD进行二次改造
  20. 如何选择最适合的房屋贷款方式

热门文章

  1. cocos2d-x游戏实例(15)-纵版射击游戏(2)
  2. HeapAlloc与malloc的区别
  3. 《认清C++语言》的random_shuffle()和transform()算法
  4. PyCairo 中的图片
  5. WiFi 热点共享设置
  6. javaScript 内存管理机制
  7. MyBatis(一)MyBatis介绍和配置详解
  8. Kafka消息序列化和反序列化(上)
  9. 赋能普通用户,面向专业级的视频内容制作技术
  10. Google Lyra Android版开源 支持3kbps语音聊天