357. Count Numbers with Unique Digits
Given a non-negative integer n, count all numbers with unique digits, x, where 0 ≤ x < 10n.

Example:
Given n = 2, return 91. (The answer should be the total numbers in the range of 0 ≤ x < 100, excluding [11,22,33,44,55,66,77,88,99])

Hint:

A direct way is to use the backtracking approach.
Backtracking should contains three states which are (the current number, number of steps to get that number and a bitmask which represent which number is marked as visited so far in the current number). Start with state (0,0,0) and count all valid number till we reach number of steps equals to 10n.
This problem can also be solved using a dynamic programming approach and some knowledge of combinatorics.
Let f(k) = count of numbers with unique digits with length equals k.
f(1) = 10, ..., f(k) = 9 * 9 * 8 * ... (9 - k + 2) [The first factor is 9 because a number cannot start with 0].

题目大意:给一个数n,求0~n位数间所有数字中,每一位数字都各不相同的数字的个数~
分析:根据提示中所给的公式,1位数字有10个,第k位有f(k) = 9 * 9 * 8 * ... (9 - k + 2)个,累加从2位到n位的f(k)的总和,再加上1位的10个数字,即为所求~

class Solution {
public:int countNumbersWithUniqueDigits(int n) {if(n == 0) return 1;int result = 10, cnt = 9;for(int i = 2; i <= n; i++) {cnt *= (11 - i);result += cnt;}return result;}
};

LeetCode 357. Count Numbers with Unique Digits相关推荐

  1. leetcode -- 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 | 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. 代码重构中的几个概念
  2. 在美人才开始流向中国,因为“硅谷太慢了”
  3. Science:人工智能的发展离不开神经科学,先天结构或是下一个方向
  4. Xamarin.iOS编译时无法连接苹果系统
  5. C#使用HttpClient进行http操作
  6. 使用过滤统计信息解决基数预估错误
  7. linux 获取计算机IP地址、MAC、操作系统、时间、DNS
  8. matlab中ifelse能嵌套吗,MATLAB嵌套if-else语句
  9. 【SpringBoot_ANNOTATIONS】生命周期 04 BeanPostProcessor 后置处理器
  10. git下载出错GnuTLS recv error (-54): Error in the pull function
  11. lqr控制和模糊控制三级倒立摆
  12. fla 优化思路 flash cs6
  13. php写登录的视频,PHP cookie实现记录用户登陆信息的方法(图文+视频)
  14. Tomcat 8.5 HTTP 报文只返回200 不返回 OK
  15. 浙江大学计算机学院师资队伍,浙江大学计算机科学与技术学院导师教师师资介绍简介-朱建科...
  16. STATA 学习笔记: outlier(离群值)的处理
  17. 定时监控Ubuntu系统HDMI热插拔进行锁屏操作
  18. 发国际快递美国专线需要注意什么事项
  19. 关于0xffffffff 到底是什么意思?
  20. linux下也有很多好游戏

热门文章

  1. 从1亿个ip中找出访问次数最多的IP
  2. PHP过滤常用标签的正则表达式
  3. 业务与技术相结合,双活体系支付架构建设
  4. ZH奶酪:Python中zip函数的使用方法
  5. 协同办公OA系统 沉淀企业文化
  6. STP RSTP MSTP PVST+学习 (1)
  7. 哪些NPM仓库更易遭供应链攻击?研究员给出了预测指标
  8. 中秋逢国庆 | 盛世华诞 阖家团圆
  9. 近300个 Windows 10 可执行文件易受 DLL 劫持攻击
  10. 为了摸清敌人对自己了解多少,高阶国家黑客组织Turla 决定偷走反病毒日志