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])

Analysis:

A number of unique digits is a number which is a combination of unrepeated digits. So, we can calculate the total number. for number with n digits, like 100-999 or 1000-9999, the total numbers with unique digits equals to 9*9*8...*(11-n). because the highest digit cannot be 0.

Here is DP. dp[i] is the count of all i-digit numbers with unique digits, dp[i] = dp[i-1]*(11-i) for i from 2 to n

 public static int countNumbersWithUniqueDigits(int n) {
if (n == 0) {
return 1;
}
int ans = 10, base = 9;
for (int i = 2; i <= n && i <= 10; i++) {
base = base * (11 - i);
ans += base;
}
return ans;
}

第一遍backtracking做法:

 public class Solution {
public int countNumbersWithUniqueDigits(int n) {
if (n == 0) return 1;
if (n == 1) return 10;
int res = 10;
for (int i=2; i<=n && i<=10; i++) {
int count = 1;
int bit = 9;
for (int j=0; j<i; j++) {
count *= bit;
if (j != 0) bit--;
}
res += count;
}
return res;
}
}

Leetcode: Count Numbers with Unique Digits的更多相关文章

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

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

  2. 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 ...

  3. 【LeetCode】357. Count Numbers with Unique Digits 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  4. 【Leetcode】357. Count Numbers with Unique Digits

    题目描述: Given a non-negative integer n, count all numbers with unique digits, x, where 0 ≤ x < 10n. ...

  5. 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. ...

  6. 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 ...

  7. [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 ...

  8. Count Numbers with Unique Digits

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

  9. 357. Count Numbers with Unique Digits

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

随机推荐

  1. Apache Kafka源码分析 - KafkaApis

    kafka apis反映出kafka broker server可以提供哪些服务,broker server主要和producer,consumer,controller有交互,搞清这些api就清楚了 ...

  2. Delphi 200X、XE中如何用并行实现循环的计算

    interface uses Classes, SysUtils; type TParallelProc = reference to procedure(i: Integer; ThreadID: ...

  3. PHP5 mysqli 教程

    mysqli提供了面向对象和面向过程两种方式来与数据库交互,分别看一下这两种方式. 1.面向对象 在面向对象的方式中,mysqli被封装成一个类,它的构造方法如下: __construct ([ st ...

  4. JS中的工厂模式

    .一个栗子: var BicycleShop = function(){}; BicycleShop.prototype = { sellBicycle : function( model ){ va ...

  5. C#窗体:关于DataGridView的数据源绑定字符串两个值得注意的问题

    无意间遇到的问题,然后就GOOGLE了下,搜到些资料,总结整理如下(注:是转载的) 1. LINQ的查询结果无法直接作为DataGridView的数据源 DataGridView的DataSource ...

  6. UI---startup--jquery

    http://www.w3school.com.cn 传统的基于表单提交, 整页刷新式的并不需要前端MVC. 当 然这种体验会很糟糕.试想一下, 用WebQQ时,每发一次消息页面就要泛白一次, 这是什 ...

  7. PHP 日期比较

    $temptime = mktime(8,2,12,4,4,2014);$dt1 = date("Y-m-d",time());$dt2 = date("Y-m-d&qu ...

  8. UIBezierPath 画线

    使用UIBezierPath类可以创建基于矢量的路径,这个类在UIKit中.此类是Core Graphics框架关于path的一个封装.使用此类可以定义简单的形状,如椭圆或者矩形,或者有多个直线和曲线 ...

  9. PMP--论文部分

    个人观点,是三个部分最简单,最容易复习的,当然,如果你没有做好一个项目的摘要和正文的背诵,连各个过程的工具输输入输出的都记不住几个,就不用往下看了,以上内容的观点,至少保证真题做三次以上,书籍教材看三 ...

  10. Appium-001-测试开发环境搭建(Android - Win7)

    随着移动端 App 测试自动化的兴起,为更好的控制产品质量,越来越多的中大型公司开始了移动端的自动化测试.Appium 自动化测试技术也是我很早之前就想学习的一门技术,却一直没有比较空余的时间来学习( ...