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:

  1. D is a subset of digits '1'-'9' in sorted order.
  2. 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的更多相关文章

  1. [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 ...

  2. LeetCode 902. Numbers At Most N Given Digit Set

    应该是常数 N的位数时间级别 我的这个方法超时很严重...但是特此记录 费劲巴拉写的... 超时: int atMostNGivenDigitSet(char** D, int DSize, int ...

  3. [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 ...

  4. 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'}.  ...

  5. 由最多N个给定数字集组成的数字 Numbers At Most N Given Digit Set

    2019-10-14 22:21:29 问题描述: 问题求解: 暴力求解必然会超时,那么就需要考虑数学的方法来降低时间复杂度了. public int atMostNGivenDigitSet(Str ...

  6. All LeetCode Questions List 题目汇总

    All LeetCode Questions List(Part of Answers, still updating) 题目汇总及部分答案(持续更新中) Leetcode problems clas ...

  7. leetcode hard

    # Title Solution Acceptance Difficulty Frequency     4 Median of Two Sorted Arrays       27.2% Hard ...

  8. F. Igor and Interesting Numbers

    http://codeforces.com/contest/747/problem/F cf #387 div2 problem f 非常好的一道题.看完题,然后就不知道怎么做,感觉是dp,但是不知道 ...

  9. About Intel® Processor Numbers

    http://www.intel.com/content/www/us/en/processors/processor-numbers.html About Intel® Processor Numb ...

随机推荐

  1. dddddddddddddddddd

    PacificA: Replication in log-based distributed storage systemshttp://research.microsoft.com:8082/pub ...

  2. (转)innodb 多版本并发控制原理详解

    转自:https://blog.csdn.net/aoxida/article/details/50689619 多版本并发控制技术已经被广泛运用于各大数据库系统中,如Oracle,MS SQL Se ...

  3. [Selenium]对弹出的Alert窗口进行操作

    Alert alert = driver.switchTo().alert(); alert.accept();

  4. 2.自己搭建的一个简易的ioc容器

    1.persondao类namespace MyselfIoC{    public class PersonDao    {        public override string ToStri ...

  5. 2018.09.09 cogs693. Antiprime数(搜索)

    传送门 看完题发现很sb. 前10个质数乘起来已经超出题目范围了. 因此只用搜索前几个质数每个的次数比较谁的因数的就行了. 代码: #include<iostream> #define l ...

  6. Linux抓包

    默认系统里边没有安装有tcpdump的,无法直接使用   这里我们可以使用yum来直接安装它 yum install -y tcpdump   如果忘记了这个软件的用法,我们可以使用 tcpdump ...

  7. html5.css3新特性

    块: article section, Nav aside header footer  内容分组 future fig caption main 文本级别标签   time i b  em  str ...

  8. POJ3258 River Hopscotch 2017-05-11 17:58 36人阅读 评论(0) 收藏

    River Hopscotch Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 13598   Accepted: 5791 ...

  9. linux处理U盘中的资料-挂载-tar.gz软件安装-linux环境下软件的安装方式

    1. U盘插入linux一般会有以下反映 (1)/dev 的目录下,多出一个sdb的磁盘. 因为:目前系统中有两个硬盘, sda是原来的系统磁盘.sdb是插入的U盘. 其中:sdb1表示sdbU盘的一 ...

  10. hdu 4993

    http://acm.hdu.edu.cn/showproblem.php?pid=4993 满足ax + by = c的x,y对数 水题,暴力 #include <cstdio> #in ...