给定正整数 N,返回小于等于 N 且具有至少 1 位重复数字的正整数。

示例 1:

输入:20
输出:1
解释:具有至少 1 位重复数字的正数(<= 20)只有 11 。

示例 2:

输入:100
输出:10
解释:具有至少 1 位重复数字的正数(<= 100)有 11,22,33,44,55,66,77,88,99 和 100 。

示例 3:

输入:1000
输出:262 数位dp可以解决,不知道怎么写。
考虑一种通解的方法:原答案即N-没有重复的数字。关键是求没有重复的数字
对于位数比N小的情况,我们可以直接排列组合求。
对于与N相同的情况,我们从最高位根据数字的限制(limit)依此排列求解
int C(int m,int n)
{
int res=;
while(n)
{
res*=m;
m--;
n--;
}
return res;
}
int numDupDigitsAtMostN(int N) {
vector<int>a; for(int x=N+;x!=;x/=)
a.push_back(x%);
int n=a.size();
reverse(a.begin(),a.end());
int res=;
for(int i=;i<n;i++)
res+=*C(,i-);
cout<<res<<endl;
map<int,int>contain;
for(int i=;i<n;i++)
{
for(int j=i>?:;j<a[i];j++)
{
if(contain[j]==)
res+=C(-i,n-i-);
}
if(contain[a[i]]>)
break;
contain[a[i]]++; }
return N-res;
}

数位dp

int dfs(int t, bool up, bool ze, bool rp, int mask) {
if (t < ) return rp;
if (!up && ~dp[t][ze][rp][mask]) return dp[t][ze][rp][mask];
int ret = , I = up ? a[t] : ;
rep(i, , I + ) {
bool nrp = rp;
int nmas = mask;
if (!(ze & i == )) {
nrp |= mask >> i & ;
nmas |= << i;
}
ret += dfs(t - , up & (i == I), ze & (i == ),
nrp, nmas);
}
if (!up) dp[t][ze][rp][mask] = ret;
return ret;
} int numDupDigitsAtMostN(int N) {
memset(dp, -, sizeof(dp));
int n = ;
while (N > ) {
a[n++] = N % ;
N /= ;
}
return dfs(n - , , , , );
}

LeetCode至 少有 1 位重复的数字的更多相关文章

  1. [Swift]LeetCode1012. 至少有 1 位重复的数字 | Numbers With 1 Repeated Digit

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

  2. LeetCode 287. Find the Duplicate Number (找到重复的数字)

    Given an array nums containing n + 1 integers where each integer is between 1 and n (inclusive), pro ...

  3. LeetCode数组中重复的数字

    LeetCode 数组中重复的数字 题目描述 在一个长度为 n 的数组 nums 里的所有数字都在 0~n-1 的范围内.数组中某些数字是重复的,但不知道有几个数字重复了,也不知道每个数字重复了几次. ...

  4. leetcode 217 Contains Duplicate 数组中是否有重复的数字

     Contains Duplicate Total Accepted: 26477 Total Submissions: 73478 My Submissions Given an array o ...

  5. Java实现 LeetCode 395 至少有K个重复字符的最长子串

    395. 至少有K个重复字符的最长子串 找到给定字符串(由小写字符组成)中的最长子串 T , 要求 T 中的每一字符出现次数都不少于 k .输出 T 的长度. 示例 1: 输入: s = " ...

  6. leetcode题库练习_数组中重复的数字

    题目:数组中重复的数字 找出数组中重复的数字. 在一个长度为 n 的数组 nums 里的所有数字都在 0-n-1 的范围内.数组中某些数字是重复的,但不知道有几个数字重复了,也不知道每个数字重复了几次 ...

  7. [LeetCode] Contains Duplicate II 包含重复值之二

    Given an array of integers and an integer k, return true if and only if there are two distinct indic ...

  8. Leetcode 137. 只出现一次的数字 II - 题解

    Leetcode 137. 只出现一次的数字 II - 题解 137. Single Number II 在线提交: https://leetcode.com/problems/single-numb ...

  9. 剑指offer(50)数组中重复的数字

    题目描述 在一个长度为n的数组里的所有数字都在0到n-1的范围内. 数组中某些数字是重复的,但不知道有几个数字是重复的.也不知道每个数字重复几次.请找出数组中任意一个重复的数字. 例如,如果输入长度为 ...

随机推荐

  1. redis 中的key值过期后,触发通知事件

    1.创建springboot工程,创建监听类 maven配置 <dependencies> <dependency> <groupId>org.springfram ...

  2. ubuntu16.04 install qtcreator

    1. 安装相关软件,搭建环境 sudo apt install qt-creator sudo apt install qt5-default source python35/bin/activate ...

  3. [UE4]Add Offset

    在原来值的基础上增加偏移

  4. atom插件

    1.Sync Settings 搭配github,同步你的atom插件信息,配置信息,让你轻松实现一台电脑配置,多台电脑共享. 2.Emmet 能够基于Emmet语法产生HTML,做过前段开发的不会不 ...

  5. 2019年1月16日22:50:28 白糖SR1905

    很好的机会,只拿了点皮毛,如果说都是因为上班时间不充裕那是给自己找借口,最主要原因没别的:思维不清,策略不明- 这里的入场初衷是周线区间下沿,日线向下脱离中枢失败后回拉一笔,那么这一单的做法就应该很明 ...

  6. nginx+多个tomcat

    学习nginx的时候遇到的问题:nginx怎么部署两台tomcat?   upstream 在网上找的资源,我在nginx配置文件(nginx.conf)中添加了两个server.结果只显示第一个se ...

  7. 位运算练习:将整数A转换为B,需要改变多少个bit位

    思路解析: 将整数A转换为B,如果A和B在第i(0<=i<32)个位上相等,则不需要改变这个BIT位,如果在第i位上不相等,则需要改变这个BIT位.所以问题转化为了A和B有多少个BIT位不 ...

  8. 域名动态解析到动态IP

    一般宽带用户的IP都是动态IP,重连之后IP可能会发生变化. 如果想在其他地方连接家里的设备,或者在家中搭建服务器,就会受到影响. 现在提供一种动态解析域名的方式,只要检测到IP的变化,那么就调用阿里 ...

  9. java-网络通信-索引

           HTTP协议 关于HTTP协议,一篇就够了 HTTP与HTTPS的区别 HTTP Keep-Alive模式 HTTP长连接和短连接 HTTP的长连接(持久连接)和短连接 HTTP的长连接 ...

  10. Jmeter5.1.1构造https请求

    1.打开浏览器,输入https的网址 2.点开浏览器前面的小锁 3.点开证书.详细信息.复制到文件 把证书保存到本地电脑 4.利用jdk中的keytool.exe工具,重新生成证书 C:\Java\j ...