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 ...
随机推荐
- Win10 激活
先看看你的WIN10激活状态:1.右键开始菜单2.运行3.slmgr.vbs -xpr KMS卸载方法:1.如果是KMSPico,则自带服务卸载批处理,2.不管是哪种KMS工具,卸载掉软件之后请执行以 ...
- python list和函数之间的复制和原地址修改问题
def change(a): a.pop() #自带的方法都是原地址修改 a=[,,] change(a) print (a)#直接修改了3. def change(a): a=[,,,] #复制操作 ...
- centos 6.5 安装mysql
步骤1: yum -y install mysql-server 步骤2: chkconfig mysqld on 步骤3: service mysqld start mysql -u root se ...
- hadoop群集 启动
###注意:严格按照下面的步骤 .5启动zookeeper集群(分别在itcast04.itcast05.itcast06上启动zk) cd /itcast/zookeeper-/bin/ ./zkS ...
- Hibernate中常见的异常处理
本文引自:http://www.blogjava.net/sy1214520/archive/2008/10/21/235667.html 本文总结Hibernate中常见的异常. 1. net.sf ...
- 如何查看路由器的mac和计算机的mac
如何查看路由器的mac和计算机的mac 一.查看路由器的mac 方法一: 直接看路由器的背面,如下图,即可看到MAC地址 打开命令提示符窗口,输入ipconfig,找到网关地址,如192.168. ...
- 【JAVA】通过URLConnection/HttpURLConnection发送HTTP请求的方法(一)
Java原生的API可用于发送HTTP请求 即java.net.URL.java.net.URLConnection,JDK自带的类: 1.通过统一资源定位器(java.net.URL)获取连接器(j ...
- DevExpress VCL 已死-----关于13.1.4的发布。
随着DevExpress VCL 13.1.4 的发布,已基本上宣布了devexpress vcl 已经死亡了. 除了一些bug 修正,没有什么新的东西,每年的订阅费又那么贵,而且delphi 现在已 ...
- 浅析10种常见的黑帽seo手法
虽然博主并不认同黑帽seo手法,但是一些常见的黑帽手法还是需要了解的,增加自己对黑帽的认知,也可以在自己优化网站时适时的规避开这些黑帽手法,从而避免自己的网站被搜索引擎惩罚.好了,话不多说,下面进入今 ...
- angularjs写公共方法
'use strict'; angular.module('fast-westone') .factory('commonUtilService', function () { return { /* ...