https://leetcode.com/problems/numbers-with-repeated-digits/

leetcode_357. Count Numbers with Unique Digits有一些相似的地方。

给定N,计算小于等于N且至少有一个重复数位的数的数目。

可转化为计算小于等于N且所有数位不相同的数的数目。且可分为两部分,

  1. 位数与N相同且小于等于N且所有数位不相同的数的数目;
  2. 位数小于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的更多相关文章

  1. 解题报告-1012. Numbers With Repeated Digits

    Given a positive integer N, return the number of positive integers less than or equal to N that have ...

  2. 【leetcode】1012. Numbers With Repeated Digits

    题目如下: Given a positive integer N, return the number of positive integers less than or equal to N tha ...

  3. Numbers With Repeated Digits

    2020-01-03 12:01:46 问题描述: 问题求解: 确实可以当作数学题去做,但是要分类讨论什么的还是有点麻烦的. 这个时候万能的dfs上场了,直接暴力检索,真的太强了. int res = ...

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

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

  5. Count Numbers with Unique Digits

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

  6. Leetcode: Count Numbers with Unique Digits

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

  7. 357. Count Numbers with Unique Digits

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

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

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

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

随机推荐

  1. Entity FramWork Code first 使用心得

    1 最有用的命令 update-database -force -verbose 2 主键如果不是默认的int或者 bigint而是guid 或者 string类型,创建记录的时候要给主键赋值 3 在 ...

  2. YTU 2562: 黄金螺旋

    2562: 黄金螺旋 时间限制: 1 Sec  内存限制: 128 MB 提交: 832  解决: 427 题目描述 黄金螺旋是根据斐波那契数列画出来的螺旋曲线,自然界中存在许多斐波那契螺旋线的图案, ...

  3. Android JNI MAC OS环境配置

    前言—JNI技术简介 JNI是Java Native Interface的缩写,即“Java本地调用”,它是Java世界和Native世界的中介桥梁.其中Native世界一般指C/C++的世界.众所周 ...

  4. Masonry tableviewCell布局

    前言 说到iOS自动布局,有很多的解决办法.有的人使用xib/storyboard自动布局,也有人使用frame来适配.对于前者,笔者并不喜欢,也不支持.对于后者,更是麻烦,到处计算高度.宽度等,千万 ...

  5. jquery datatable无数据提示不居中显示

    原文地址:https://www.jianshu.com/p/fc4784d11722 昨天遇到一个问题,datatable生成的表格没有数据,但是“No data found”没有居中,根本原因是c ...

  6. CI框架中一个类中调用另一个类中已经加载对象测试

    controller.php <?php class CI_Controller { private static $instance; public function __construct( ...

  7. input type="file"文件上传到后台读取

    html页面(表单采用bootStrap) js部分: //更换头像时把上传的图片post方式到控制器 <script type="text/javascript"> ...

  8. oracle ORA-01704: string literal too long问题分析

    今天使用sql在oracle直接insert update一个表时,出现ORA-01704: string literal too long的错误,我们的sql是 update mall_config ...

  9. [SDOI2013]保护出题人

    题目 出题人铭铭认为给SDOI2012出题太可怕了,因为总要被骂,于是他又给SDOI2013出题了. 参加SDOI2012的小朋友们释放出大量的僵尸,企图攻击铭铭的家.而你作为SDOI2013的参赛者 ...

  10. linux下的c程序排版工具:indent 分类: linux 2014-06-14 20:05 720人阅读 评论(0) 收藏

    Linux下有一个方便的c语言程序排版工具,只要选择恰当的参数,可以轻易地使自己的程序具有统一的风格. 当然首先要安装indent,执行命令:apt-get install indent indent ...