博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
LeetCode-Count Numbers with Unique Digits
阅读量:4155 次
发布时间:2019-05-25

本文共 671 字,大约阅读时间需要 2 分钟。

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

Constraints:

0 <= n <= 8

分析

F(0) = 1
F(1) = 10
F(2) = 10 +9(第一个数不能为0)9(可以从0~9中选出一个任意一个不与第一位相同的数)
F(3) = 10 +9
9 + 998
F(4) = …

F(n) = F(n-1) + 9 * 8 * …* 9-n+1;

public int countNumbersWithUniqueDigits(int n) {
if(n == 0) return 1; int sum = 10; int step = 9; int K = 9; for( int i = 0 ; i < n-1;i ++){
step = step *(K -i); sum += step; } return sum; }

转载地址:http://cvwxi.baihongyu.com/

你可能感兴趣的文章
彻底了解mapreduce核心Shuffle--解惑各种mapreduce问题
查看>>
mysql主从复制实现数据库同步
查看>>
CentOS7配置VNC
查看>>
kvm虚拟机磁盘扩容
查看>>
带你搞懂朴素贝叶斯分类算法
查看>>
jvm调优
查看>>
【Tomcat】Tomcat配置JVM参数步骤
查看>>
centos7安装配置mysql
查看>>
Hadoop中的文件格式
查看>>
《转》Linux文件特殊权限——SetUID、SetGID、Sticky BIT
查看>>
Linux权限管理
查看>>
Java并发编程:volatile关键字解析
查看>>
java中CountDownLatch、CyclicBarrier和Semaphore
查看>>
java中的string.intern()
查看>>
Google protobuffer序列化工具使用以及与idea集成
查看>>
Applet与javacript通信
查看>>
Scala泛型详解
查看>>
“二马”的战争 马云:微信不代表未来
查看>>
一度的领军者 横扫千军的AMD
查看>>
我走了,青春留给北京
查看>>