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'}
. (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 toN
.
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:
D
is a subset of digits'1'-'9'
in sorted order.1 <= N <= 10^9
Approach #1: Math. [C++]
class Solution {
public:
int atMostNGivenDigitSet(vector<string>& D, int N) {
string s = to_string(N);
int n = s.length();
int ans = 0;
for (int i = 1; i < n; ++i)
ans += pow(D.size(), i);
for (int i = 0; i < n; ++i) {
bool prefix = false;
for (string d : D) {
if (d[0] < s[i]) {
ans += pow(D.size(), n-i-1);
} else if (d[0] == s[i]) {
prefix = true;
break;
}
} if (!prefix) return ans;
} return ans + 1;
}
};
Reference:
https://zxi.mytechroad.com/blog/math/leetcode-902-numbers-at-most-n-given-digit-set/
902. Numbers At Most N Given Digit Set的更多相关文章
- [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'}. (Not ...
- LeetCode 902. Numbers At Most N Given Digit Set
应该是常数 N的位数时间级别 我的这个方法超时很严重...但是特此记录 费劲巴拉写的... 超时: int atMostNGivenDigitSet(char** D, int DSize, int ...
- [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 ...
- 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'}. ...
- 由最多N个给定数字集组成的数字 Numbers At Most N Given Digit Set
2019-10-14 22:21:29 问题描述: 问题求解: 暴力求解必然会超时,那么就需要考虑数学的方法来降低时间复杂度了. public int atMostNGivenDigitSet(Str ...
- All LeetCode Questions List 题目汇总
All LeetCode Questions List(Part of Answers, still updating) 题目汇总及部分答案(持续更新中) Leetcode problems clas ...
- leetcode hard
# Title Solution Acceptance Difficulty Frequency 4 Median of Two Sorted Arrays 27.2% Hard ...
- F. Igor and Interesting Numbers
http://codeforces.com/contest/747/problem/F cf #387 div2 problem f 非常好的一道题.看完题,然后就不知道怎么做,感觉是dp,但是不知道 ...
- About Intel® Processor Numbers
http://www.intel.com/content/www/us/en/processors/processor-numbers.html About Intel® Processor Numb ...
随机推荐
- git format-patch 用法【转】
本文转载自:http://blog.csdn.net/xzongyuan/article/details/9425739 git format-patch相对于git diff更便于操作,是更新的打包 ...
- 搭建事务管理转账案例的环境(强调:简化开发,以后DAO可以继承JdbcDaoSupport类)
1. 步骤一:创建WEB工程,引入需要的jar包 * IOC的6个包 * AOP的4个包 * C3P0的1个包 * MySQL的驱动包 * JDBC目标2个包 * 整合JUnit测试包2.步骤二:创建 ...
- C#调用默认浏览器打开网页的几种方法
private void button1_Click(object sender, EventArgs e) { //从注册表中读取默认浏览器可执行文件路径 RegistryKey key = Reg ...
- ffmpeg 入门教程
1.在terminal,安装ffmpeg开源库 #brew install ffmpeg 2.新建一个目录first_ffmpeg,并在first_ffmpeg目录下新建ffmpeg目录 3.拷贝/u ...
- 彻底关闭win10的自动更新,一定要坚持看到最后一页
https://jingyan.baidu.com/article/9faa7231e7b78b473c28cbb6.html 是否好使我还在测试,不好使的话,再跟新 2018-05-20 今天电脑又 ...
- 按条件设置gridcontrol 单元格属性
等价代码设置:(注意这里使用都是FieldName, 即绑定对象的字段名) var cn = new StyleFormatCondition(FormatConditionEnum.Expressi ...
- angularjs写公共方法
'use strict'; angular.module('fast-westone') .factory('commonUtilService', function () { return { /* ...
- Nginx中间件使用心得(一)
一.Nginx简介 1.什么是Nginx? Nginx是一个高效.可靠的web服务器和反向代理中间件. (高效:支持海量并发请求,可靠:可靠运行的) 2.Nginx地位? 在https:// ...
- python读取文件另存为
fr = open(filename_r,encoding='cp852') w2 = open(filename_w,'a')#a代表追加 w代表重写 for line in fr: w2.writ ...
- Win7_Ultimate + VS2010 + openGL 配置
Win7_Ultimate + VS2010 + openGL 配置 0. 前言 OpenGL作为当前主流的图形API之一,它在一些场合具有比DirectX更优越的特性. (1)与C语言紧密结合. O ...