[LeetCode] 902. Numbers At Most N Given Digit Set 最大为 N 的数字组合
We have a sorted set of digits `D`, a non-empty subset of `{'1','2','3','4','5','6','7','8','9'}`. (Note that `'0'` is not included.)
Now, we write numbers using these digits, using each digit as many times as we want. For example, if D = {'1','3','5'}, we may write numbers such as '13', '551', '1351315'.
Return the number of positive integers that can be written (using the digits of D) that are less than or equal to N.
Example 1:
Input: D = ["1","3","5","7"], N = 100
Output: 20
Explanation:
The 20 numbers that can be written are:
1, 3, 5, 7, 11, 13, 15, 17, 31, 33, 35, 37, 51, 53, 55, 57, 71, 73, 75, 77.
Example 2:
Input: D = ["1","4","9"], N = 1000000000
Output: 29523
Explanation:
We can write 3 one digit numbers, 9 two digit numbers, 27 three digit numbers,
81 four digit numbers, 243 five digit numbers, 729 six digit numbers,
2187 seven digit numbers, 6561 eight digit numbers, and 19683 nine digit numbers.
In total, this is 29523 integers that can be written using the digits of D.
Note:
Dis a subset of digits'1'-'9'in sorted order.1 <= N <= 10^9
这道题给了我们一个有序字符串数组,里面是0到9之间的数(这里博主就纳闷了,既然只有一位数字,为啥不用 char 型,而要用 string 型),然后又给了一个整型数字N,问无限制次数使用D中的任意个数字,能组成多个不同的小于等于D的数字。先来分析例子1,当N为 100 时,所有的一位数和两位数都是可以的,既然可以重复使用数字,假设总共有n个数字,那么对于两位数来说,十位上和个位上分别都有n种可能,总共就是 n^2 种可能,对于一位数来说,总共n种可能。那么看到这里就可以归纳出当N总共有 len 位的话,我们就可以快速的求出不超过 len-1 位的所有情况综合,用个 for 循环,累加n的指数即可。然后就要来分析和数字N位数相等的组合,题目中的两个的例子的N都是1开始的,实际上N可以是任何数字,举个例子来说吧,假如 D={"1","3","5","7"},N=365,那么根据前面的分析,我们可以很快的算出所有的两位数和一位数的组合情况总数 4 + 4^2 = 20 个。现在要来分析三位数都有哪些组合,由于D数组是有序的,所以我们从开头遍历的话先取到的就是最小的,这时候有三种情况,小于,等于,和大于,每种的处理情况都有些许不同,这里就拿上面提到的例子进行一步一步的分析:
- 对于N的百位数字3来说,D中的1小于N中的百位上的3,那么此时百位上固定为1,十位和个位上就可以是任意值了,即 1xx,共有 4^2 = 16 个。
- 对于N的百位数字3来说,D中的3等于N中的百位上的3,那么此时百位上固定为3,十位和个位的值还是不确定,此时就不能再继续遍历D中的数字了,因为之后的数字肯定大于3,但是我们可以继续尝试N的下一位。
- 对于N的十位数字6来说,D中的1小于N中的十位上的6,那么百位和十位分别固定为3和1,个位上就可以是任意值了,即 31x,共有 4 个。
- 对于N的十位数字6来说,D中的3小于N中的十位上的6,那么百位和十位分别固定为3和3,个位上就可以是任意值了,即 33x,共有 4 个。
- 对于N的十位数字6来说,D中的5小于N中的十位上的6,那么百位和十位分别固定为3和5,个位上就可以是任意值了,即 35x,共有 4 个。
- 对于N的十位数字6来说,D中的7大于N中的十位上的6,此时再也组不成小于N的数字了,直接返回最终的 20+16+4+4+4=48 个。
代码如下:
class Solution {
public:
int atMostNGivenDigitSet(vector<string>& D, int N) {
string str = to_string(N);
int res = 0, n = D.size(), len = str.size();
for (int i = 1; i < len; ++i) res += pow(n, i);
for (int i = 0; i < len; ++i) {
bool hasSameNum = false;
for (string &d : D) {
if (d[0] < str[i]) res += pow(n, len - 1 - i);
else if (d[0] == str[i]) hasSameNum = true;
}
if (!hasSameNum) return res;
}
return res + 1;
}
};
Github 同步地址:
https://github.com/grandyang/leetcode/issues/902
参考资料:
https://leetcode.com/problems/numbers-at-most-n-given-digit-set/
https://leetcode.com/problems/numbers-at-most-n-given-digit-set/discuss/225123/c%2B%2B100
[LeetCode All in One 题目讲解汇总(持续更新中...)](https://www.cnblogs.com/grandyang/p/4606334.html)
[LeetCode] 902. Numbers At Most N Given Digit Set 最大为 N 的数字组合的更多相关文章
- LeetCode 902. Numbers At Most N Given Digit Set
应该是常数 N的位数时间级别 我的这个方法超时很严重...但是特此记录 费劲巴拉写的... 超时: int atMostNGivenDigitSet(char** D, int DSize, int ...
- 902. Numbers At Most N Given Digit Set
We have a sorted set of digits D, a non-empty subset of {'1','2','3','4','5','6','7','8','9'}. (Not ...
- [Swift]LeetCode902. 最大为 N 的数字组合 | Numbers At Most N Given Digit Set
We have a sorted set of digits D, a non-empty subset of {'1','2','3','4','5','6','7','8','9'}. (Not ...
- [LeetCode] Count Numbers with Unique Digits 计算各位不相同的数字个数
Given a non-negative integer n, count all numbers with unique digits, x, where 0 ≤ x < 10n. Examp ...
- [LeetCode] Consecutive Numbers 连续的数字
Write a SQL query to find all numbers that appear at least three times consecutively. +----+-----+ | ...
- Leetcode: Count Numbers with Unique Digits
Given a non-negative integer n, count all numbers with unique digits, x, where 0 ≤ x < 10n. Examp ...
- [LeetCode] Consecutive Numbers 连续的数字 --数据库知识(mysql)
1. 题目名称 Consecutive Numbers 2 .题目地址 https://leetcode.com/problems/consecutive-numbers/ 3. 题目内容 写一个 ...
- LeetCode902. Numbers At Most N Given Digit Set
题目: We have a sorted set of digits D, a non-empty subset of {'1','2','3','4','5','6','7','8','9'}. ...
- [LeetCode] Lexicographical Numbers 字典顺序的数字
Given an integer n, return 1 - n in lexicographical order. For example, given 13, return: [1,10,11,1 ...
随机推荐
- 一.OS运行机制
运行机制: 1.中断(外部) =====一种通知行为(例如插入键盘) 2.系统调用(主动反应) ===一种请求行为 3.异常(内部) =====一种错误处理行为 系统调用和程序接口的关系,接口把系统调 ...
- Netty中FastThreadLocal源码分析
Netty中使用FastThreadLocal替代JDK中的ThreadLocal[JAVA]ThreadLocal源码分析,其用法和ThreadLocal 一样,只不过从名字FastThreadLo ...
- Prometheus监控学习笔记之Prometheus如何热加载更新配置
0x00 概述 当 Prometheus 有配置文件修改,我们可以采用 Prometheus 提供的热更新方法实现在不停服务的情况下实现配置文件的重新加载. 0x01 热更新 热更新加载方法有两种: ...
- 你不知道的Golang map
在开发过程中,map是必不可少的数据结构,在Golang中,使用map或多或少会遇到与其他语言不一样的体验,比如访问不存在的元素会返回其类型的空值.map的大小究竟是多少,为什么会报"can ...
- Vertx和Jersey集成使用
为了更好地解耦和提高性能,一般将工程的接口部分剥离出来形成一个单独的工程,这样不仅能提高性能,增强可维护性,并且在后台工程宕掉的话对客户端接口的影响较小. 公司使用了Vertx和Jersey,Vert ...
- Python如何实现单例模式?其他23中设计模式python如何实现?
单例模式主要有四种方法:new.共享属性.装饰器.import. # __ new__方法: class Singleton(object): def __new__(cls, *args, **kw ...
- jmeter对tomcat性能测试
主要对tomcat的参数做一些记录(jmeter和tomcat在同一个计算机,可能引起测试误差) 我的计算机配置 4核8线程 8G内存 案例一 tomcat JVM 1.8G堆内存,无数据库操 ...
- 构建maven项目,自定义目录结构方法
构建maven项目 创建自定义的文件目录方法: 在项目名称右键-->Builder Path-->Configure Builder Path...Source菜单下的Add Folder ...
- i春秋暑期训练营丨渗透测试工程师开课啦
每个人的夏天 都有专属的解锁方式 或来一次难忘的旅行 或躺在家里吹着空调吃西瓜 又或者是和小伙伴参加暑期训练营 i春秋暑期渗透测试工程师 报名通道已全部开启 为了保证课程质量,采取小班教学,每班仅限3 ...
- odoo10学习笔记十六:定时任务
转载请注明原文地址:https://www.cnblogs.com/ygj0930/p/11189382.html 一:定义定时器数据模型 模型中定义需要用到的字段.定时方法 from odoo im ...