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:
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
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/count-numbers-with-unique-digits
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
我的思路1:(暴力)
1、最直接的就是暴力解,但是超时。到7的时候就超时了
class Solution {
public:int countNumbersWithUniqueDigits(int n) {int len =pow(10,n);int count = 0;for(int i = 0; i < len; i++){if(check(i) == false) count++;}return len - count;}bool check(int n){ //如果有重复,返回falsevector<bool> vis(10, false);if(n == 0) return true;while(n != 0){int yushu = n % 10;if(vis[yushu] == false){vis[yushu] = true;}else return false;n /= 10;}return true;}
};
参考思路2:(dp)
1、dp[i]表示i位数字不同的数字数
class Solution {
public:int countNumbersWithUniqueDigits(int n) {if(n == 0) return 1;int dp[11];dp[0] = 1;int per = 1;for(int i = 1; i <= 10; i++){dp[i] = dp[i-1] + 9*per;per = per * (10-i);}if(n <= 10) return dp[n];else return dp[10];}};
leetcode -- 357. Count Numbers with Unique Digits相关推荐
- LeetCode 357. Count Numbers with Unique Digits
357. Count Numbers with Unique Digits Given a non-negative integer n, count all numbers with unique ...
- leetcode 357. Count Numbers with Unique Digits | 357. 计算各个位数不同的数字个数(Java)
题目 https://leetcode.com/problems/count-numbers-with-unique-digits/ 题解 只要认真分析数字特点,这题不难,就是个带一些条件的排列组合. ...
- 357. Count Numbers with Unique Digits
357. Count Numbers with Unique Digits Given a non-negative integer n, count all numbers with unique ...
- [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(计数问题)
题意:给出一个整数,求在[0,10^n]之间各位数字不相同的数的个数 思路:n为1时,有10个 n为2时, 十位数只能是1-9,而个数位可以为0,所以也是有9种选择 n为3时,百位数有9种选择,十位数 ...
- mysql unique count_MySQL - Count Number of Unique Values
问题 If I have three columns: orderNumber, name, email and I would like to count how many unique email ...
- pandas使用nunique函数计算dataframe每个数据列的独特值的个数(count number of unique values in each column of dataframe)
pandas使用nunique函数计算dataframe每个数据列的独特值的个数(count number of unique values in each column of dataframe) ...
- LeetCode 829. Consecutive Numbers Sum--笔试题--C++解法
LeetCode 829. Consecutive Numbers Sum–笔试题–C++解法 LeetCode题解专栏:LeetCode题解 LeetCode 所有题目总结:LeetCode 所有题 ...
- LeetCode 38. Count and Say
问题链接 LeetCode 38. Count and Say 题目解析 找规律,每一个数字串是上一个数字串的"读法".比如:n=1时为"1",读作" ...
最新文章
- android学习笔记34——ClipDrawable资源
- Concert Tour(Uvalive 6853)
- 汇编语言---键盘KeyCode值列表
- msgpack pack php,msgpack_pack和redis的诡异事
- Ionic Mac 环境配置
- centos7.4编译mysql5.6,centos7编译安装mysql5.6
- 操作系统——文件的逻辑结构
- php面向对象编程代码怎么写,php面向对象编程(一)
- labview的介绍,安装和卸载
- 一个C++工程CPU占用100%问题的排查
- 2020 年微服务领域开源数字化报告发布,阿里扛起微服务大旗
- 获取CPUID序列号的两种办法
- 安居客头部买房部分书写
- 南方cass10.1中文版
- 神经网络前景背景分析,人工神经网络研究背景
- Windows 10搭建SVN服务器
- 一些触动人心的动效设计欣赏
- 沪深股票接口如何获取所有股票代码?
- cad抠图 lisp_用Autolisp对AutoCAD进行二次改造
- 如何选择最适合的房屋贷款方式