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.
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])
解题思路:
- 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].
代码如下:
public class Solution {
public int countNumbersWithUniqueDigits(int n) {
if(n == 0)
return 1;
int temp = 9;
int res = 0;
for(int i = 2; i <= n; i++){
temp *= (9 - i + 2);
res += temp;
}
return res + 10;
}
}
Java [Leetcode 357]Count Numbers with Unique Digits的更多相关文章
- LC 357. Count Numbers with Unique Digits
Given a non-negative integer n, count all numbers with unique digits, x, where 0 ≤ x < 10n. Examp ...
- 【LeetCode】357. Count Numbers with Unique Digits 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【Leetcode】357. Count Numbers with Unique Digits
题目描述: Given a non-negative integer n, count all numbers with unique digits, x, where 0 ≤ x < 10n. ...
- 357. Count Numbers with Unique Digits
Given a non-negative integer n, count all numbers with unique digits, x, where 0 ≤ x < 10n. Examp ...
- 357 Count Numbers with Unique Digits 计算各个位数不同的数字个数
给定一个非负整数 n,计算各位数字都不同的数字 x 的个数,其中 0 ≤ x < 10n.示例:给定 n = 2,返回 91.(答案应该是除[11,22,33,44,55,66,77,88,99 ...
- [LeetCode] Count Numbers with Unique Digits 计算各位不相同的数字个数
Given a non-negative integer n, count all numbers with unique digits, x, where 0 ≤ x < 10n. Examp ...
- Leetcode: Count Numbers with Unique Digits
Given a non-negative integer n, count all numbers with unique digits, x, where 0 ≤ x < 10n. Examp ...
- Count Numbers with Unique Digits -- LeetCode
Given a non-negative integer n, count all numbers with unique digits, x, where 0 ≤ x < 10^n. Exam ...
- [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 ...
随机推荐
- NGUI 3.50 UIButton使用
在NGUI,3.X的版本中,取消了创建UIbutton这个选项,所以我们可以创建uisprite.uilabel等,然后在上面附加uibutton脚本,达到目的,具体步骤 1:在界面上键好2D或3D ...
- LeetCode: Max Consecutive Ones
这题最关键的是处理最开始连续1和最后连续1的方式,想到list一般在最前面加个node的处理方式,在最前面和最后面加0即可以很好地处理了 public class Solution { public ...
- Microsoft.VisualStudio.Web.PageInspector.Loader
未能加载文件或程序集"Microsoft.VisualStudio.Web.PageInspector.Loader, Version=1.0.0.0, Culture=neutral, P ...
- 二叉树、平衡二叉树、B-Tree与B+Tree
本文总结自:https://blog.csdn.net/chuixue24/article/details/80027689 二叉树(B树,binary tree) 左子树的键值 < 根的键值 ...
- FAQ | 是什么导致MySQL数据库服务器磁盘I/O高(本文章来自知数堂)
FAQ | 是什么导致MySQL数据库服务器磁盘I/O高 2016-12-26 叶金荣 老叶茶馆 0.导读 有个MySQL服务器的磁盘I/O总有过高报警,怎么回事? 本文约1500字,阅读时间约10分 ...
- 判断当前html是否在微信中打开
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name ...
- vim与shell终端操作技巧
一.快速注释多行代码 1.添加//注释符 :10,50s#^#//#g 使用#作为分隔符 2.删除//注释符 :10,50s#^//##g 使用#作为分隔符 3.添加#注释符 ...
- jquery的50个免费插件
说不定什么时候用到呢 http://www.admin10000.com/document/4711.html
- GO学习笔记:struct的匿名字段
我们上面介绍了如何定义一个struct,定义的时候是字段名与其类型一一对应,实际上Go支持只提供类型,而不写字段名的方式,也就是匿名字段,也称为嵌入字段. 当匿名字段是一个struct的时候,那么这个 ...
- 5.8 页面对象(Page Object)模式
页面对象(Page Object)模式是目前自动化测试领域普遍使用的设计模式之一,此模式可以大大提高测试代码的复用率,提高测试脚本的编写效率和维护效率,是中级自动化测试工程师的必备技能之一. 1.页面 ...