在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★ Given a positive integer Example 1: Input: 20
Output: 1
Explanation: The only positive number (<= 20) with at least 1 repeated digit is 11.
Example 2: Input: 100
Output: 10
Explanation: The positive numbers (<= 100) with atleast 1 repeated digit are 11, 22, 33, 44, 55, 66, 77, 88, 99, and 100.
Example 3: Input: 1000
Output: 262
Note:
给定正整数 示例 1: 输入:20 输出:1 解释:具有至少 1 位重复数字的正数(<= 20)只有 11 。 示例 2: 输入:100 输出:10 解释:具有至少 1 位重复数字的正数(<= 100)有 11,22,33,44,55,66,77,88,99 和 100 。 示例 3: 输入:1000 输出:262 提示:
Runtime: 4 ms
Memory Usage: 19.1 MB
1 class Solution { 2 func numDupDigitsAtMostN(_ N: Int) -> Int { 3 var L:[Int] = [Int]() 4 var x:Int = N + 1 5 while(x > 0) 6 { 7 L.insert(x % 10,at:0) 8 x /= 10 9 } 10 var res:Int = 0 11 var n:Int = L.count 12 for i in 1..<n 13 { 14 res += 9 * A(9, i - 1) 15 } 16 var seen:Set<Int> = Set<Int>() 17 for i in 0..<n 18 { 19 var j:Int = i > 0 ? 0 : 1 20 while(j < L[i]) 21 { 22 if !seen.contains(j) 23 { 24 res += A(9 - i, n - i - 1) 25 } 26 j += 1 27 } 28 if seen.contains(L[i]) {break} 29 seen.insert(L[i]) 30 } 31 return N - res 32 } 33 34 func A(_ m:Int,_ n:Int) -> Int 35 { 36 return n == 0 ? 1 : A(m, n - 1) * (m - n + 1) 37 } 38 }
|
请发表评论