★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公衆號:山青詠芝(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:函數
D
is a subset of digits '1'-'9'
in sorted order.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 個整數。
提示:
D
是按排序順序的數字 '1'-'9'
的子集。1 <= N <= 10^9
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 }
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 }