[LeetCode] 902. Numbers At Most N Given Digit Set 最大爲 N 的數字組合

<br> 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.)html

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'.git

Return the number of positive integers that can be written (using the digits of D) that are less than or equal to N.github

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:less

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:this

  1. D is a subset of digits '1'-'9' in sorted order.
  2. 1 <= N <= 10^9

<br> 這道題給了咱們一個有序字符串數組,裏面是0到9之間的數(這裏博主就納悶了,既然只有一位數字,爲啥不用 char 型,而要用 string 型),而後又給了一個整型數字N,問無限制次數使用D中的任意個數字,能組成多個不一樣的小於等於D的數字。先來分析例子1,當N爲 100 時,全部的一位數和兩位數都是能夠的,既然能夠重複使用數字,假設總共有n個數字,那麼對於兩位數來講,十位上和個位上分別都有n種可能,總共就是 n^2 種可能,對於一位數來講,總共n種可能。那麼看到這裏就能夠概括出當N總共有 len 位的話,咱們就能夠快速的求出不超過 len-1 位的全部狀況綜合,用個 for 循環,累加n的指數便可。而後就要來分析和數字N位數相等的組合,題目中的兩個的例子的N都是1開始的,實際上N能夠是任何數字,舉個例子來講吧,假如 D={"1","3","5","7"},N=365,那麼根據前面的分析,咱們能夠很快的算出全部的兩位數和一位數的組合狀況總數 4 + 4^2 = 20 個。如今要來分析三位數都有哪些組合,因爲D數組是有序的,因此咱們從開頭遍歷的話先取到的就是最小的,這時候有三種狀況,小於,等於,和大於,每種的處理狀況都有些許不一樣,這裏就拿上面提到的例子進行一步一步的分析:code

  • 對於N的百位數字3來講,D中的1小於N中的百位上的3,那麼此時百位上固定爲1,十位和個位上就能夠是任意值了,即 1xx,共有 4^2 = 16 個。
  • 對於N的百位數字3來講,D中的3等於N中的百位上的3,那麼此時百位上固定爲3,十位和個位的值仍是不肯定,此時就不能再繼續遍歷D中的數字了,由於以後的數字確定大於3,可是咱們能夠繼續嘗試N的下一位。
  • 對於N的十位數字6來講,D中的1小於N中的十位上的6,那麼百位和十位分別固定爲3和1,個位上就能夠是任意值了,即 31x,共有 4 個。
  • 對於N的十位數字6來講,D中的3小於N中的十位上的6,那麼百位和十位分別固定爲3和3,個位上就能夠是任意值了,即 33x,共有 4 個。
  • 對於N的十位數字6來講,D中的5小於N中的十位上的6,那麼百位和十位分別固定爲3和5,個位上就能夠是任意值了,即 35x,共有 4 個。
  • 對於N的十位數字6來講,D中的7大於N中的十位上的6,此時再也組不成小於N的數字了,直接返回最終的 20+16+4+4+4=48 個。

代碼以下:htm

<br>blog

class Solution {
public:
    int atMostNGivenDigitSet(vector<string>& D, int N) {
        string str = to_string(N);
        int res = 0, n = D.size(), len = str.size();
        for (int i = 1; i < len; ++i) res += pow(n, i);
        for (int i = 0; i < len; ++i) {
            bool hasSameNum = false;
            for (string &d : D) {
                if (d[0] < str[i]) res += pow(n, len - 1 - i);
                else if (d[0] == str[i]) hasSameNum = true;
            }
            if (!hasSameNum) return res;
        }
        return res + 1;
    }
};

<br> Github 同步地址:three

https://github.com/grandyang/leetcode/issues/902

<br> 參考資料:

https://leetcode.com/problems/numbers-at-most-n-given-digit-set/

https://leetcode.com/problems/numbers-at-most-n-given-digit-set/discuss/225123/c%2B%2B100

https://leetcode.com/problems/numbers-at-most-n-given-digit-set/discuss/168439/C%2B%2B-O(logN)-Clear-code-with-explanation

<br> [LeetCode All in One 題目講解彙總(持續更新中...)](https://www.cnblogs.com/grandyang/p/4606334.html)

相關文章
相關標籤/搜索