在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★ We have a sorted set of digits Now, we write numbers using these digits, using each digit as many times as we want. For example, if Return the number of positive integers that can be written (using the digits of Example 1: Input: D = 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 = 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 = ["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 个整数。 提示:
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 }
|
请发表评论