给定一个非负整数 n,计算各位数字都不同的数字 x 的个数,其中 0 ≤ x < 10n。
示例:
给定 n = 2,返回 91。(答案应该是除[11,22,33,44,55,66,77,88,99]外,0 ≤ x < 100 间的所有数字)

详见:https://leetcode.com/problems/count-numbers-with-unique-digits/description/

C++:

class Solution {
public:
int countNumbersWithUniqueDigits(int n) {
if (n == 0)
{
return 1;
}
int res = 0;
for (int i = 1; i <= n; ++i)
{
res += count(i);
}
return res;
}
int count(int k)
{
if (k < 1)
{
return 0;
}
if (k == 1)
{
return 10;
}
int res = 1;
for (int i = 9; i >= (11 - k); --i)
{
res *= i;
}
return res * 9;
}
};

参考:https://www.cnblogs.com/grandyang/p/5582633.html

357 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. 357. Count Numbers with Unique Digits

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

  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. 【LeetCode】357. Count Numbers with Unique Digits 解题报告(Python & C++)

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

  7. Java实现 LeetCode 357 计算各个位数不同的数字个数

    357. 计算各个位数不同的数字个数 给定一个非负整数 n,计算各位数字都不同的数字 x 的个数,其中 0 ≤ x < 10n . 示例: 输入: 2 输出: 91 解释: 答案应为除去 11, ...

  8. leetcode 357. 计算各个位数不同的数字个数(DFS,回溯,数学)

    题目链接 357. 计算各个位数不同的数字个数 题意: 给定一个非负整数 n,计算各位数字都不同的数字 x 的个数,其中 0 ≤ x < 10n . 示例: 输入: 2 输出: 91 解释: 答 ...

  9. Leetcode 357.计算各个位数不同的数字个数

    计算各个位数不同的数字个数 给定一个非负整数 n,计算各位数字都不同的数字 x 的个数,其中 0 ≤ x < 10n . 示例: 输入: 2 输出: 91 解释: 答案应为除去 11,22,33 ...

随机推荐

  1. java8 新特性 lambda过滤

    1. 定义实体类 package com.atguigu.java8; public class Employee { private int id; private String name; pri ...

  2. https://blog.csdn.net/u011495642/article/details/79958444

    https://blog.csdn.net/u011495642/article/details/79958444--- 一文读懂大数据平台——写给大数据开发初学者的话! 史上最详细的Hadoop环境 ...

  3. 根据判断数组不为空然后取他的值----数组不会为空---只能判断其size是否大于0

    private List<Integer> classId=new ArrayList<Integer>(); business.getClassId()!=null 以上为错 ...

  4. 不动点(Fixed Point)

    本系列文章由 @yhl_leo 出品,转载请注明出处. 文章链接: http://blog.csdn.net/yhl_leo/article/details/51735818 在数学中,函数的不动点( ...

  5. [luoguP2158] [SDOI2008]仪仗队(数论)

    传送门 可以看出 (i, j) 能被看到,(i * k, j * k) 都会被挡住 暴力 所以 gcd(i, j) == 1 的话 ans ++ 那么可以枚举一半(中轴对称),求解答案,只能拿30分 ...

  6. spoj 375 树链剖分模板

    /* 只是一道树链刨分的入门题,作为模板用. */ #include<stdio.h> #include<string.h> #include<iostream> ...

  7. 超级钢琴(codevs 2934)

    题目描述 Description 小Z是一个小有名气的钢琴家,最近C博士送给了小Z一架超级钢琴,小Z希望能够用这架钢琴创作出世界上最美妙的音乐. 这架超级钢琴可以弹奏出n个音符,编号为1至n.第i个音 ...

  8. [bzoj3781]小B的询问_莫队

    小B的询问 bzoj-3781 题目大意:给定一个n个数的序列,m次询问.每次询问一段区间内数的种类的平方和. 注释:$1\le n\,m\le 5\cdot 10^4$. 想法:莫队练习题. 我们考 ...

  9. POJ 1386 Play on Words(有向欧拉通路 连通图)

    题意  见下方中文翻译 每一个单词能够看成首尾两个字母相连的一条边  然后就是输入m条边  推断是否能构成有向欧拉通路了 有向图存在欧拉通路的充要条件: 1. 有向图的基图连通: 2. 全部点的出度和 ...

  10. 架构师速成6.7-设计开发思路-uml

    uml是什么东西?统一建模语言.一门语言.是用来进行软件设计的一门语言. 事实上一门语言的诞生并不伟大,让大多数人都使用才足够伟大. uml就是一门伟大的语言.由于眼下软件设计的唯一语言就是它. UM ...