leetcode_1015. Numbers With Repeated Digits
https://leetcode.com/problems/numbers-with-repeated-digits/
与leetcode_357. Count Numbers with Unique Digits有一些相似的地方。
给定N,计算小于等于N且至少有一个重复数位的数的数目。
可转化为计算小于等于N且所有数位不相同的数的数目。且可分为两部分,
- 位数与N相同且小于等于N且所有数位不相同的数的数目;
- 位数小于N且所有数位不相同的数的数目。
难点在于第1部分。
解法一:
使用dfs计算第一部分。时间上开销相对较大

class Solution{
public:
int res_=, numofdigist_=;
bool flag[];
int digists[];
void dfs(int numofdigist, int digist, bool smaller){
if(numofdigist == ){
if(!smaller){
for(int i=; i<=digist; i++)
if(flag[i]==)
res_++;
}
else{
for(int i=; i<=; i++)
if(flag[i]==)
res_++;
}
return;
}
for(int i=; i<=; i++){
if(i==&&numofdigist==numofdigist_)
continue;
if(i>digist && smaller==)
break;
if(flag[i]==){
flag[i]=;
if(!smaller&&i<digist)
dfs(numofdigist-, digists[numofdigist-], );
else if(!smaller&&i==digist)
dfs(numofdigist-, digists[numofdigist-], );
else if(smaller)
dfs(numofdigist-, digists[numofdigist-], );
flag[i]=;
}
}
}
int numDupDigitsAtMostN(int N){
if(N<=)
return ;
int n=N;
int weight=;
while(N>){
digists[numofdigist_++] = N%;
N/=;
}
memset(flag, , sizeof(flag));
dfs(numofdigist_, digists[numofdigist_-], );
for(int i=;i<=numofdigist_-;i++){
int tmp=;
for(int j=;j<i;j++)
tmp *= -j+;
res_+=tmp;
}
return n-res_;
}
};
解法二:
从最高位开始,计算当前i位相同情况下小于等于N且所有数位不相同的数的数目。

class Solution{
public:
int res_=, numofdigist_=;
bool flag[];
int digists[];
int calc(int num, int wei){ //计算还剩num个数没用,还剩wei位有多少种情况
int res=;
for(int i=; i<wei; i++)
res *= num-i;
return res;
}
int numDupDigitsAtMostN(int N){
if(N<=)
return ;
N++; //计算小于N的数目,更方便处理
int n=N;
int weight=;
while(N>){ //计算出N的位数和每一位的数值
digists[numofdigist_++] = N%;
N/=;
}
//计算第1部分
memset(flag,,sizeof(flag));
res_ = (digists[numofdigist_-]-)*calc(,numofdigist_-);//第1位不能为0
flag[digists[numofdigist_-]]=;
for(int i=numofdigist_-; i>=; i--){
for(int j=; j<digists[i]; j++)
if(flag[j]==)
res_ += calc(-numofdigist_+i+, i);
if(flag[digists[i]]==)
break;
flag[digists[i]]=;
}
//计算第2部分
for(int i=; i<=numofdigist_-; i++){
int tmp=;
for(int j=; j<i; j++)
tmp *= -j+;
res_+=tmp;
}
return n--res_;
}
leetcode_1015. Numbers With Repeated Digits的更多相关文章
- 解题报告-1012. Numbers With Repeated Digits
Given a positive integer N, return the number of positive integers less than or equal to N that have ...
- 【leetcode】1012. Numbers With Repeated Digits
题目如下: Given a positive integer N, return the number of positive integers less than or equal to N tha ...
- Numbers With Repeated Digits
2020-01-03 12:01:46 问题描述: 问题求解: 确实可以当作数学题去做,但是要分类讨论什么的还是有点麻烦的. 这个时候万能的dfs上场了,直接暴力检索,真的太强了. int res = ...
- [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
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 ...
- 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
题目描述: Given a non-negative integer n, count all numbers with unique digits, x, where 0 ≤ x < 10n. ...
- [leetcode-357-Count Numbers with Unique Digits]
Given a non-negative integer n, count all numbers with unique digits, x, where 0 ≤ x < 10n. Examp ...
随机推荐
- xpath中双斜杠的作用// double slash
https://stackoverflow.com/questions/36019544/if-double-slash-is-used-2-times-in-xpath-what-does-it-m ...
- YTU 2920: Shape系列-7
2921: Shape系列-7 时间限制: 1 Sec 内存限制: 128 MB 提交: 156 解决: 129 题目描述 小强做的Shape类在本次的测试中出了点状况,发现原来是其中的area函 ...
- 一步一步学Silverlight 2系列(5):实现简单的拖放功能
述 Silverlight 2 Beta 1版本发布了,无论从Runtime还是Tools都给我们带来了很多的惊喜,如支持框架语言Visual Basic, Visual C#, IronRuby, ...
- Java 并发 —— 读写锁(ReadWriteLock)
读写锁(ReadWriteLock),顾名思义,就是在读写某文件时,对该文件上锁. 1. ReentrantReadWriteLock 三部曲: 加锁: 读写操作: 解锁:(为保证解锁操作一定执行,通 ...
- java对象序列化的理解
1.java中的序列化时transient变量(这个关键字的作用就是告知JAVA我不可以被序列化)和静态变量不会被序列 化(下面是一个测试的例子) (实体带versionUUID,便 ...
- Python3中 对local和nonlocal 关键字的改善认识(新手向)
nonlocal关键字用来在函数或其他作用域中使用外层(非全局)变量. nonlocal用于声明,修改嵌套作用域(enclosing 作用域,外层非全局作用域)中的变量,如下实例: #!/usr/bi ...
- VS2013Xml文件节点导航插件开发
一.功能描述 该插件的功能跟代码文件的导航功能类似,只是下拉框里的内容是元素的某一属性值,如图-1所示 图-1 当点击下拉框的选项后,会自动定位到该内容在xml文件的位置.此功能适用于xml文件内容较 ...
- centos配置nodejs和mysql
我使用的是centos7.2 64位,弄了一大晚上试了各种方法,安装的nodejs就是启动不了服务器.全是IP能ping通,浏览器不能访问.端口都是打开了的.安全组也设置了,就是不行.最后阿里云客服电 ...
- Ruby Numeric类
Numeric类 Numeric ---------> Integer ---------> Fixnum ...
- bzoj 2618【半平面交模板】
#include<iostream> #include<cstdio> #include<algorithm> #include<cmath> usin ...