题目:

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

题意理解:

给了你一个string数组D,和一个数字N。D中每个string包含的是一个数字字符。相当于就是D中包含了LD个一位数字,然后用这些数字进行组合,只要比N小就满足条件,问一共有多少种满足条件的组合可能。

DP思想:

对于D的组合,当组合的数字长度小于N的长度时,很好理解,就是D的size的i次幂相加,1 <= i < D;

当组合的数字长度等于N的长度时,情况就有点复杂了,但是我们可以通过DP的思想来理解。

即对于数字N从右往左扫描,如果对于一个扫描的点,再遍历D。

1.如果D中的一个数字小于N中的当前值,那么当前位置的组合数就加上D长度的一个次幂,具体多少次幂,取决于N中当前位置,相当于就是 1XXX, 2abc 组合数就是D长度的3次幂。

2.如果D中的一个数字等于N中的当前值,那么就加上前一个位置的组合数,相当于就是 1XXXX 和 1XXXX;组合数取决于后4位。

3.如果D中的一个数字比N中的当前值大,其实不用处理,因为我们设定的DP数组初始值都是0;

需要注意的就是DP数组要多开一个位置,存储最后一个值1,自己随便写一个就知道原因了。

代码:

class Solution {
public:
int atMostNGivenDigitSet(vector<string>& D, int N) {
string s = to_string(N);
int LD = D.size();
int LN = s.size();
int ans = 0;
int DP[LN+1];
memset(DP, 0, sizeof(DP));
DP[LN] = 1; for(int i = LN-1; i >= 0; i--)
{
int nt = s[i] - '0';
for(int j = 0; j < LD; j++)
{
if(D[j][0] - '0' == nt)
DP[i] += DP[i+1];
else if(D[j][0] - '0' < nt)
DP[i] += pow(LD, LN - i - 1);
} } for(int j = 1; j < LN; j++)
{ DP[0] += pow(LD, j);
} return DP[0];
}
};

  

LeetCode902. Numbers At Most N Given Digit Set的更多相关文章

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

  2. 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 ...

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

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

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

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

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

  6. F. Igor and Interesting Numbers

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

  7. About Intel® Processor Numbers

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

  8. ural 1289. One Way Ticket

    1289. One Way Ticket Time limit: 1.0 secondMemory limit: 64 MB A crowed of volunteers dressed in the ...

  9. CSUFT 1003 All Your Base

    1003: All Your Base Time Limit: 1 Sec      Memory Limit: 128 MB Submit: 4      Solved: 2 Description ...

随机推荐

  1. HtmlHelper扩展

    扩展 HtmlHelper类 public static class MyHtmlHelper { //扩展方法 //静态类,静态方法,this关键字 //调用方法<%=Html.MyLabel ...

  2. selenium2 用Yaml文件进行元素管理 (五)

    比如界面有一个按钮,id号是test.如果进行对象化的话,就是test.click就可以了.不用每次都要去创建test对象.如果id号变了,我们也只需要改一下test的名称就行了. 使用Yaml需要用 ...

  3. Codeforces 1109C 线段树

    题意及思路:https://www.cnblogs.com/TinyWong/p/10400682.html 代码: #include <bits/stdc++.h> #define ls ...

  4. java代理模式与装饰者模式

    静态代理和装饰者模式的区别: 先来看一下装饰者模式的定义:装饰者模式动态地将责任附加到对象上.若要扩展功能,装饰者提供了比继承更有弹性的替代方案. 总结一下采用装饰者模式是为了增强或拓展原对象的功能. ...

  5. 生成ico格式图标

    ico格式可参考如下链接: http://msdn.microsoft.com/en-us/library/ms997538.aspx http://en.wikipedia.org/wiki/ICO ...

  6. c++策略模式(Strategy Method)

    别人的博客再讲策略模式时都会讲三国,策略类就是赵云的锦囊,锦囊里装着若干妙计.在打仗时想要用什么妙计,直接从锦囊里去取. 锦囊类: class context { public: context(IS ...

  7. springMVC+spring+mybatis整合(包括文件上传和下载)

    driver=com.mysql.jdbc.Driver url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncod ...

  8. 2-javascript::笔记

    0.位置: HTML 中的脚本必须位于 <script> 与 </script> 标签之间. 脚本可被放置在 HTML 页面的 <body> 和 <head& ...

  9. Django开发——集成的子框架django.contrib

    Django开发——集成的子框架django.contrib 2018年09月11日 19:32:42 Mrkang1314 阅读数:63  https://blog.csdn.net/mashaok ...

  10. javascript总结16:数组array12

    1 Array 对象 作用:Array 对象用于在变量中存储多个值. 1.1 数组定义 var ary = new Array();//通过创建对象的方式创建数组 var ary1 = [];// 直 ...