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 ...
随机推荐
- XMU 1050 Diffuse Secret 【最短路】
1050: Diffuse Secret Time Limit: 500 MS Memory Limit: 64 MBSubmit: 10 Solved: 8[Submit][Status][We ...
- c# 32位机和64位机 读取Excel内容到DataSet
// ----------------------32位机 //注释说明 //ExclePath 为Excel路径 批号 是指Excel文件中某一列必填项 public static DataSet ...
- mipi屏在内核可以显示logo但是u-boot无法显示的问题【转】
本文转载自:http://blog.csdn.net/fulinus/article/details/45071721 平台:瑞芯的rk3288 u-boot版本:u-boot-2014.10 ker ...
- Ubuntu系统下实现Android工程调用独立编译的C++程序和GMP、PBC库
目的: 实现使用C++编写代码供Android工程调用.C++代码中可以使用STL库,也可以使用常用的由源码编译生成的库,如PBC.因为PBC是基于GMP库的,所以这里只记录了GMP和PBC库的编译安 ...
- BZOJ_2081_[Poi2010]Beads_哈希
BZOJ_2081_[Poi2010]Beads_哈希 Description Zxl有一次决定制造一条项链,她以非常便宜的价格买了一长条鲜艳的珊瑚珠子,她现在也有一个机器,能把这条珠子切成很多块(子 ...
- WebRTC PeerConnection
PeerConnection 是WebRtc native interface 最外层的模块, 通常来说, 用户使用Peerconection就可以构建最简单的p2p 应用. 下面的UML里 ...
- CGAN 论文笔记
<Conditional Generative Adversarial Nets>: 发表于2014 computer Science.作者提出了最简单的条件控制GAN模型,在D和G两个模 ...
- UI:数据的解析XML与JSON
XML 和 JSON 语言 本篇博客来自互联网参考 XML 和 JSON 的互相转化 有属性的转化为对象,无属性的转化为字符串 节点的顺序性不可逆,XML有顺序,JSON 无顺序 XML 和 J ...
- C# 反射 设置实体属性
C# 反射 设置实体属性 http://blog.csdn.net/cestarme/article/details/6548126 C#反射设置属性值和获取属性值 http://www.cnblog ...
- java翻译lua+c+openssl签名项目
原来项目中用openresty nginx+lua实现server,lua调用c动态链接库,来使用openss做签名,并生成130字节(128签名+2位自定义字节)长度的文件. nginx: loca ...