给定正整数 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. Kong(v1.0.2)认证

    介绍 上游服务(api或微服务)的流量通常由各种Kong的authentication plugins的应用程序和配置控制.由于Kong的服务实体表示您自己的上游服务的一对一映射,所以最简单的场景是在 ...

  2. Java文件File类学习总结

    java.io.File类 代表文件和目录,在开发中,读取文件.生成文件.删除文件.修改文件的属性都会用到该类. 常见构造方法: public File(String pathName){} 以pat ...

  3. 了解原型设计工具pencil project

    Pencil Pencil是一个功能强大的界面原型设计工具,可以设计网页和桌面程序界面,侧重点在于设计,支持自定义控件和导出模板,功能确实很强大. 内置形状集合 从2.0.2开始铅笔默认包含更多形状集 ...

  4. sqoop的安装

    Sqoop是一个用来完成Hadoop和关系型数据库中的数据相互转移的工具, 他可以将关系型数据库(MySql,Oracle,Postgres等)中的数据导入Hadoop的HDFS中, 也可以将HDFS ...

  5. Java笔记Spring(九)

    完整调试springmvc源码 WebApplicationContext = new XmlWebApplicationContext();// XmlWebApplicationContext通过 ...

  6. python-web自动化-鼠标操作

    鼠标操作由ActionChains类来完成鼠标操作 perform() 执行鼠标操作move_to_element() 鼠标悬浮:最常用的操作double_click() 双击操作context_cl ...

  7. 类名:IExternalCommandAvailability+IExternalCommand实现对某些控件的自定义使用

    起初对于这些名词不懂,后经查阅了解如下,希望对学习者能有所帮助.在Revil里大部分命令在没有打开文档的时候是禁用的,有的在没有打开文档也是可以使用的.而又一些在平面视图是禁用的如标高,有的在3D视图 ...

  8. gogs windows

    首先安装 git,然后下载 gogs. 在gogs 文件夹位置 启动. gogs.exe web 打开浏览器,输入 127.0.0.1:3000 ,安装 gogs,注意数据库选择,仓库根目录,管理员帐 ...

  9. PG数据库中用户权限

    每当在数据库中创建一个对象,所有者可以分配它访问权限.所有者通常是执行创建语句的用户.对于大多数类型的对象,初始状态是,只有所有者(或超级用户)可以修改或删除对象.为了让其他角色或用户使用它,特权或权 ...

  10. LeetCode 144. Binary Tree Preorder Traversal 二叉树的前序遍历 C++

    Given a binary tree, return the preorder traversal of its nodes' values. Example: Input: [,,] \ / Ou ...