给定正整数 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. SfMLearner 记录

    2019年3月2日09:29:54 正在看SfMLearner的pytorch源码,意识到无监督的深度估计最重要的是利用实体的一致性 来建立loss. 对于一个不移动的物体,相机从一个pose到另一个 ...

  2. python之路——22

    学习内容 1.初识面向对象 类:抽象的,模子 对象:具体的,根据类规范 代码精简,修改方便,属性规范2.对象 查看属性 调用方法 __dict__,增删改查,通过字典语法进行3.类名 1.实例化 2. ...

  3. 基于STM8的UART发送和中断接收---STM8-第二章

    1. 综述 UART的基础知识,通用异步收发传输器(Universal Asynchronous Receiver/Transmitter),通常称作UART,是一种异步收发传输器. 做软件开发的人都 ...

  4. jar包冲突排解方法(sbt)

    jar包依赖冲突,版本不兼容会导致各种各样的问题.这里推荐一款sbt插件用于查找项目中的jar包依赖关系,通过该插件可以轻松的看出某个jar包依赖哪些jar,以及某个jar被哪些jar所依赖.此外该插 ...

  5. Linux内存解读

    1.free -m命令 [root@crawler ~]# free -m total used free shared buffers cached Mem: -/+ buffers/cache: ...

  6. 傻瓜学编程之block_1

    1.引出 1.1.int pp = 3;for循环为dd赋值并 调用 finprint(int ,int)函数 获取pp*dd的乘积,输出 void finalValue(int d, int sca ...

  7. 地下产链——创建安装包捆绑软件(Bundled software)

    Bundled_Software 首先,因为个人知识不足的情况下,无法进行EXE文件捆绑机的制作说明,所以有需要请转至http://www.cnblogs.com/qintangtao/archive ...

  8. [Vue warn]: Duplicate keys detected: '1'. This may cause an update error

    今天遇到这个问题,遇到这个问题多数因为:key值的问题 第一种情况(key重复) <div class="name-list" v-for="(item,index ...

  9. Nginx 负载配置

    简版的,详细参数需要自己微调. nginx.conf http{ upstream name { server 127.0.0.1:8777; server 127.0.0.1:8778; serve ...

  10. 解决oracle11G密码过期问题

    Oracle提示错误消息ORA-28001: the password has expired,是由于Oracle11G的新特性所致, Oracle11G创建用户时缺省密码过期限制是180天(即6个月 ...