LeetCode至 少有 1 位重复的数字
给定正整数 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 位重复的数字的更多相关文章
- [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 ...
- LeetCode 287. Find the Duplicate Number (找到重复的数字)
Given an array nums containing n + 1 integers where each integer is between 1 and n (inclusive), pro ...
- LeetCode数组中重复的数字
LeetCode 数组中重复的数字 题目描述 在一个长度为 n 的数组 nums 里的所有数字都在 0~n-1 的范围内.数组中某些数字是重复的,但不知道有几个数字重复了,也不知道每个数字重复了几次. ...
- leetcode 217 Contains Duplicate 数组中是否有重复的数字
Contains Duplicate Total Accepted: 26477 Total Submissions: 73478 My Submissions Given an array o ...
- Java实现 LeetCode 395 至少有K个重复字符的最长子串
395. 至少有K个重复字符的最长子串 找到给定字符串(由小写字符组成)中的最长子串 T , 要求 T 中的每一字符出现次数都不少于 k .输出 T 的长度. 示例 1: 输入: s = " ...
- leetcode题库练习_数组中重复的数字
题目:数组中重复的数字 找出数组中重复的数字. 在一个长度为 n 的数组 nums 里的所有数字都在 0-n-1 的范围内.数组中某些数字是重复的,但不知道有几个数字重复了,也不知道每个数字重复了几次 ...
- [LeetCode] Contains Duplicate II 包含重复值之二
Given an array of integers and an integer k, return true if and only if there are two distinct indic ...
- Leetcode 137. 只出现一次的数字 II - 题解
Leetcode 137. 只出现一次的数字 II - 题解 137. Single Number II 在线提交: https://leetcode.com/problems/single-numb ...
- 剑指offer(50)数组中重复的数字
题目描述 在一个长度为n的数组里的所有数字都在0到n-1的范围内. 数组中某些数字是重复的,但不知道有几个数字是重复的.也不知道每个数字重复几次.请找出数组中任意一个重复的数字. 例如,如果输入长度为 ...
随机推荐
- Windows下Mysql5.7忘记root密码的解决方法
找到mysql5.7安装的目录,我这里是:C:\Program Files\MySQL\MySQL Server 5.7 先停止mysql服务 在服务里找到MySQL57服务,停掉它. 修改my.in ...
- nexus3.14.0版本linux环境安装、启动、搭建私库
本文介绍的是nexus3.14.0版本在linux环境下安装.启动.搭建私库. nexus3以上的版本太新了,网上很少介绍安装细节的.据了解和2.X版本有所不同了. 1.前提 linux机器上需先安装 ...
- 7.6 chcount.c -- 使用逻辑与运算符
include <stdio.h> #define PERIOD '.' int main(void) { char ch; int charcount = 0; while ((ch = ...
- Python【每日一问】14
问:请介绍一下Python中的 import 机制 答: import 语句结合了两个操作:1.它先搜索指定名称的模块 2.将搜索结果绑定到当前作用域中的名称. 如果指定名称的模块未找到,则会引发 M ...
- C++ 64位操作系统调用 RegOpenKey() 读取注册表,返回 2, ERROR_FILE_NOT_FOUND
环境:64位操作系统, VS2017 首先在命令行执行 REG ADD HKLM\Software\seastarsun /v serial /t REG_SZ /d 58ae4cb077a4e1 在 ...
- 部分视图 - partial
对于partia来说,可以理解为组件化的运用,即将对应的html/js/css进行封装,然后通过模板引擎直接进行调用 1.partial的注册 //可以直接写在app.js,也可以写在之前所说的hel ...
- dubbo 调用服务超时
先贴出错误报告: Failed to invoke the method *** in the service ***. Tried times of the providers [] (/) on ...
- Mock Server
下载地址:https://repo1.maven.org/maven2/com/github/dreamhead/moco-runner/ 此处使用standalone的方式,不使用api用法 启动方 ...
- 理解Linux文件权限
任何完整的系统都应该具备有某种形式的安全性.必须用过某种机制来保护文件不被未授权的用户查看或修改:Linux系统遵循了Unix的文件权限的方法,来根据用户与用户组授权,实现文件安全访问. 1.Linu ...
- scrapy使用指南
创建scrapy项目: scrapy startproject 项目名 cd到项目名下 scrapy genspider 爬虫名 www.baidu.com(网站网址) 之后按照提示创建爬虫文件(官方 ...