[Swift]LeetCode1012. 至少有 1 位重复的数字 | Numbers With 1 Repeated Digit
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(shanqingyongzhi)
➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/)
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址: https://www.cnblogs.com/strengthen/p/10546296.html
➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
Given a positive integer N
, return the number of positive integers less than or equal to N
that have at least 1 repeated digit.
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 <= N <= 10^9
给定正整数 N
,返回小于等于 N
且具有至少 1 位重复数字的正整数。
示例 1:
输入:20 输出:1 解释:具有至少 1 位重复数字的正数(<= 20)只有 11 。
示例 2:
输入:100 输出:10 解释:具有至少 1 位重复数字的正数(<= 100)有 11,22,33,44,55,66,77,88,99 和 100 。
示例 3:
输入:1000 输出:262
提示:
1 <= N <= 10^9
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 }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了