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. 使用C#开发HTTP服务器系列之实现Get和Post

     各位朋友大家好,我是秦元培,欢迎大家关注我的博客,我的博客地址是http://qinyuanpei.com.在我们这个Web服务器有了一个基本的门面以后,我们是时候来用它做点实际的事情了.还记得我们 ...

  2. 牛客练习赛13D:幸运数字Ⅳ(康托展开) F:关键字排序

    链接:https://www.nowcoder.com/acm/contest/70/D 题目: 定义一个数字为幸运数字当且仅当它的所有数位都是4或者7. 比如说,47.744.4都是幸运数字而5.1 ...

  3. Junit常用操作

    断言方法: public static void assertEquals(Object[] expected, Object[] actual) // 测试a是否等于b public static ...

  4. js中的call和apply方法

    call方法: 语法:call(thisObj,[arg1,arg2,arg3,...]); 定义:调用一个对象的一个方法,以另一个对象替换当前对象. 说明: call 方法可以用来代替另一个对象调用 ...

  5. UI:沙盒

    IOS平台下,沙盒的本质就是一个文件夹 每一款IOS应用安装在手机上都会自动的生成一个文件夹.之所以叫沙盒,就是因为这个文件夹是每次运行随机产生的文件夹.沙盒文件夹是独立的,每个应用之间不能互相访问. ...

  6. Java Socket实战之一:单线程通信

    转自:http://developer.51cto.com/art/201202/317543.htm 现在做Java直接使用Socket的情况是越来越少,因为有很多的选择可选,比如说可以用sprin ...

  7. MD5 密码加密算法 系统等待

    MD5 密码加密算法 public static String md(String md, String pass) { MessageDigest m; String passok = " ...

  8. J20170524-hm

    取りこぼし 意外地输给较自己实力弱的对手,爆出冷门,败给手下败将 振り分け 分配,整理 スキーマ 图解.模式.图式

  9. P3755 [CQOI2017]老C的任务

    传送门 可以离线,把询问拆成四个,然后把所有的按\(x\)坐标排序,这样就只要考虑\(y\)坐标了.然后把\(y\)坐标离散化,用树状数组统计即可 记得开longlong //minamoto #in ...

  10. (11)用css设计电子相册 {上}

    本篇学习资料讲解:       通过css对电子相册进行排版 和 侧面强调“盒子模型.标准流.浮动和定位”的重要性. 先来看看"双向联动模式"的电子相册图: {鼠标指针经过某张图片 ...