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 ...
随机推荐
- sublime text3使用插件SublimeTextTrans设置透明度
推荐一款在windows上设置sublime2和sublime3背景透明度的插件:SublimeTextTrans, 下载地址:https://github.com/vhanla/SublimeTex ...
- YTU 2953: A代码填充--学画画
2953: A代码填充--学画画 时间限制: 1 Sec 内存限制: 128 MB 提交: 62 解决: 52 题目描述 最近小平迷上了画画,经过琨姐的指导,他学会了RGB色彩的混合方法.对于两种 ...
- 织梦DEDE系统跨站跨数据库调用数据显示
调用方法 本标签的调用格式为: {dede:sql sql="一条完整的SQL语句" appname="数据库配置参数"}您的底层模板{/dede:sql} 稍 ...
- 比特币客户端bitcoind的高级用法
Bitcoin 比特币官方客户端有两个版本:一个是图形界面的版本,通常被称为 Bitcoin(首字母大写),以及一个简洁命令行的版本(称为 bitcoind).它们相互间是兼容的,有着同样的命令行参数 ...
- 安装tensorflow的最简单方法(Ubuntu 16.04 && CentOS)
先说点题外话:在用anaconda安装很多次tensorflow失败之后,我放弃了,如果你遇到这样的问题:Traceback (most recent call last)-如果不是因为pip版本,就 ...
- r.json()
requests模块中,r.json()为Requests中内置的JSON解码器 其中只有response返回为json格式时,用r.json()打印出响应的内容, 如果response返回不为jso ...
- win7上安装macaca的报错问题
macaca网上的各种教程中,都建议使用淘宝源安装macaca,使用淘宝源就需要先安装cnpm,在win7上切换到淘宝源安装cnpm后(npm install -g cnpm --registry=h ...
- softmax regression in c++
#include <iostream>#include <vector>#include <cmath>#include <algorithm>#inc ...
- C语言的随机发牌程序(红桃、黑桃、梅花、方块)
做一个随机发牌的C语言程序,供大家学习,思考. 未做任何注释,有测试时候留下的一些输出语句,一遍方便测试. /* author:nunu qq:398269786 */ #include<std ...
- 设计模式-Template Method Pattern
将generic部份放在abstract base class中的实现的方法中,而将和具体context相关的部份作为abstract base class的虚方法,由derivatives去实现. ...