给定一个非负整数 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. noip模拟赛 三角形

    [问题描述] 平面上有N条直线,用方程Aix + Biy +Ci =0表示.这些直线没有三线共点的.现在要你计算出用这些直线可以构造出多少三角形? 输入: 第1行:一个整数N(1 ≤ N≤ 30000 ...

  2. 生产(production)

    [题目描述] 工厂为了生产一种复杂的产品,给各个生产部门制定了详细的生产计划.那么,就经常会有生产部门要把产品送到另一个生产部门作为原料.这是一个注重产品质量的工厂,所以每当有产品要从A部门运到B部门 ...

  3. codevs1197 Vigenère密码

    题目描述 Description 16 世纪法国外交家Blaise de Vigenère设计了一种多表密码加密算法——Vigenère密码.Vigenère 密码的加密解密算法简单易用,且破译难度比 ...

  4. 五语言学习系列 C,C++,Objective-C,Java,C# (一)历史

    C:由AT&T贝尔实验室的Dennis Ritchie于1972年创建的,是专为开发者设计的语言. C++:在C基础上,1983年又由贝尔实验室的Bjarne Strou-strup推出了C+ ...

  5. nyoj_600_花儿朵朵_201404162000

    花儿朵朵 时间限制:1000 ms  |  内存限制:65535 KB 难度:5   描述 春天到了,花儿朵朵盛开,hrdv是一座大花园的主人,在他的花园里种着许多种鲜花,每当这个时候,就会有一大群游 ...

  6. wait、notify应用场景(生产者-消费者模式)

    Java实现生产者消费者的方式有:wait && notify.BlockingQueue.Lock && Condition等 wait.notify注意事项:(1) ...

  7. Swift之闭包

    swift中闭包是一个非常强大的东西,闭包是自包括的函数代码块,能够在代码中被传递和使用.跟C 和 Objective-C 中的代码块(blocks)非常相似 .这个大家必须掌握!必须掌握! 必须掌握 ...

  8. vmware 10虚拟机安装ubuntu server 14.04

    -開始安装ubuntu server- 1.首先是安装系统时的语言,推荐选择English 然后安装系统 这里选择语言非常重要,我開始选择了中文,安装完毕后会乱码,所以推荐选择英文 由于在中国.所以选 ...

  9. 性能测试实战-XYB项目-内网访问

    使用内网服务器,linux host绑定域名,相当于ip地址+域名的host绑定,只不过这里的ip是yc-sparks.schoolis.cn

  10. leetcode笔记:Wiggle Sort

    一. 题目描写叙述 Given an unsorted array nums, reorder it in-place such that nums[0] <= nums[1] >= nu ...