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 ...
随机推荐
- jquery正则判断字符串有几个逗号
var angelweb="我,你,ta,";var re=/[,,]/g;if(re.test(angelweb)){ var n=angelweb.match(re).leng ...
- css布局---各种居中
居中是我们使用css来布局时常遇到的情况.使用css来进行居中时,有时一个属性就能搞定,有时则需要一定的技巧才能兼容到所有浏览器,本文就居中的一些常用方法做个简单的介绍. 注:本文所讲方法除了特别说明 ...
- windows,phalcon工具的安装使用
一.使用工具之前,必须安装phalcon的扩展,也就是php_phalcon.dll动态链接库 phalcon官方地址:https://github.com/phalcon/cphalcon/rele ...
- linux下memcache安装
安装配置 1. 安装libevent # tar zxf libevent-1.4.6-stable.tar.gz # cd libevent-1.4.6-stable # ./configure # ...
- Extended Backus–Naur Form
From Wikipedia, the free encyclopedia In computer science, Extended Backus–Naur Form (EBNF) is a fam ...
- 连接redis
- qt基本类
多firstpage secondpage thirdpage fouthpage 实现 多页面 xml解析 实现 按钮 和 slot实现 mysql数据库访问实现
- 【原型实战】分分钟搞定Unsplash网站原型设计
网站原型设计是我们在设计网页过程中必不可少的一步,激烈的市场竞争让我们不得不对产品进行快速迭代,如何高速有效的进行原型设计成为了设计师头疼的问题.本文将以unsplash网站为实例,教大家快速搞定we ...
- 【Linux】SVN的安装和配置
SVN SVN:SVN是Subversion的简称,是一种开放代码的版本控制系统,相比较RCS.CVS,它采用了分支管理系统,它的设计目标就是取代CVS.互联网上很多版本控制器服务已从CVS迁移到Su ...
- Mac下PHP+MySQL+Apache2环境搭建
本机系统信息如下: -------------------------------------------------------------------------------------- OS: ...