[Swift]LeetCode902. 最大为 N 的数字组合 | Numbers At Most N Given Digit Set

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(shanqingyongzhi)
➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址: http://www.javashuo.com/article/p-uvnsjmin-me.html 
➤若是连接不是山青咏芝的博客园地址,则多是爬取做者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持做者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★html

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

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

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: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:函数

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

咱们有一组排序的数字 D,它是  {'1','2','3','4','5','6','7','8','9'} 的非空子集。(请注意,'0' 不包括在内。)this

如今,咱们用这些数字进行组合写数字,想用多少次就用多少次。例如 D = {'1','3','5'},咱们能够写出像 '13', '551', '1351315' 这样的数字。spa

返回能够用 D 中的数字写出的小于或等于 N 的正整数的数目。 code

示例 1:

输入:D = ["1","3","5","7"], N = 100
输出:20
解释:
可写出的 20 个数字是:
1, 3, 5, 7, 11, 13, 15, 17, 31, 33, 35, 37, 51, 53, 55, 57, 71, 73, 75, 77.

示例 2:

输入:D = ["1","4","9"], N = 1000000000
输出:29523
解释:
咱们能够写 3 个一位数字,9 个两位数字,27 个三位数字,
81 个四位数字,243 个五位数字,729 个六位数字,
2187 个七位数字,6561 个八位数字和 19683 个九位数字。
总共,可使用D中的数字写出 29523 个整数。 

提示:

  1. D 是按排序顺序的数字 '1'-'9' 的子集。
  2. 1 <= N <= 10^9

Runtime: 8 ms
Memory Usage: 19.6 MB
 1 class Solution {
 2     func atMostNGivenDigitSet(_ D: [String], _ N: Int) -> Int {
 3         var NS:String = String(N)
 4         var digit:Int = NS.count
 5         var dsize:Int = D.count
 6         var rtn:Int = 0
 7         for i in 1..<digit
 8         {
 9             rtn += Int(pow(Double(dsize), Double(i)))
10         }
11         for i in 0..<digit
12         {
13             var hasSameNum:Bool = false
14             for d in D
15             {
16                 if d[0] < NS[i]
17                 {
18                     rtn += Int(pow(Double(dsize), Double(digit - i - 1)))
19                 }
20                 else if d[0] == NS[i]
21                 {
22                     hasSameNum = true
23                 }
24             }
25             if !hasSameNum {return rtn}
26         }
27         return rtn + 1
28     }
29 }
30 
31 //String扩展
32 extension String {        
33     //subscript函数能够检索数组中的值
34     //直接按照索引方式截取指定索引的字符
35     subscript (_ i: Int) -> Character {
36         //读取字符
37         get {return self[index(startIndex, offsetBy: i)]}
38     }
39 }

Runtime: 8 ms
Memory Usage: 20.2 MB
 1 class Solution {
 2     func atMostNGivenDigitSet(_ D: [String], _ N: Int) -> Int {
 3         let nums = D.map { (item) -> Int in return Int(item)! }.sorted()
 4         var number = N / 10
 5         var digitNumber = 1
 6         var disgits = [N % 10]
 7         while number != 0 {
 8             digitNumber += 1
 9             disgits.insert(number % 10, at: 0)
10             number = number / 10
11             
12         }
13         var count = 0
14         for i in 1..<digitNumber {
15             count += Int(pow(Double(nums.count), Double(i)))
16         }
17         count += hightDigitNumber(nums: nums, disgits: disgits)
18         for i in 0..<disgits.count {
19             var isBreak = true
20             for num in nums {
21                 if num == disgits[i] {
22                     if i == disgits.count - 1 {
23                         count += 1
24                     }else {
25                         count += hightDigitNumber(nums: nums, disgits: Array(disgits[i + 1..<disgits.count]))
26                     }
27                     isBreak = false
28                 }
29             }
30             if isBreak {
31                 break;
32             }
33         }
34         return count
35     }
36     func hightDigitNumber(nums:[Int],disgits:[Int]) -> Int {
37         var lessCount = 0
38         for num in nums {
39             if num < disgits[0] {
40                 lessCount += 1
41             }
42         }
43         return Int(pow(Double(nums.count), Double(disgits.count - 1))) * lessCount
44     }
45 }
相关文章
相关标签/搜索