[Swift]LeetCode788. 旋转数字 | Rotated Digits
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(shanqingyongzhi)
➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/)
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址: https://www.cnblogs.com/strengthen/p/10545646.html
➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
X is a good number if after rotating each digit individually by 180 degrees, we get a valid number that is different from X. Each digit must be rotated - we cannot choose to leave it alone.
A number is valid if each digit remains a digit after rotation. 0, 1, and 8 rotate to themselves; 2 and 5 rotate to each other; 6 and 9 rotate to each other, and the rest of the numbers do not rotate to any other number and become invalid.
Now given a positive number N
, how many numbers X from 1
to N
are good?
Example: Input: 10 Output: 4 Explanation: There are four good numbers in the range [1, 10] : 2, 5, 6, 9. Note that 1 and 10 are not good numbers, since they remain unchanged after rotating.
Note:
- N will be in range
[1, 10000]
.
我们称一个数 X 为好数, 如果它的每位数字逐个地被旋转 180 度后,我们仍可以得到一个有效的,且和 X 不同的数。要求每位数字都要被旋转。
如果一个数的每位数字被旋转以后仍然还是一个数字, 则这个数是有效的。0, 1, 和 8 被旋转后仍然是它们自己;2 和 5 可以互相旋转成对方;6 和 9 同理,除了这些以外其他的数字旋转以后都不再是有效的数字。
现在我们有一个正整数 N
, 计算从 1
到 N
中有多少个数 X 是好数?
示例: 输入: 10 输出: 4 解释: 在[1, 10]中有四个好数: 2, 5, 6, 9。 注意 1 和 10 不是好数, 因为他们在旋转之后不变。
注意:
- N 的取值范围是
[1, 10000]
。
1 class Solution { 2 func rotatedDigits(_ N: Int) -> Int { 3 var res:Int = 0 4 var dp:[Int] = [Int](repeating:0,count:N + 1) 5 for i in 0...N 6 { 7 if i < 10 8 { 9 if i == 0 || i == 1 || i == 8 10 { 11 dp[i] = 1 12 } 13 else if i == 2 || i == 5 || i == 6 || i == 9 14 { 15 dp[i] = 2 16 res += 1 17 } 18 } 19 else 20 { 21 var a:Int = dp[i / 10] 22 var b:Int = dp[i % 10] 23 if a == 1 && b == 1 24 { 25 dp[i] = 1 26 } 27 else if a >= 1 && b >= 1 28 { 29 dp[i] = 2 30 res += 1 31 } 32 } 33 } 34 return res 35 } 36 }
12ms
1 class Solution { 2 3 func rotatedDigits(_ N: Int) -> Int { 4 var count = 0 5 for n in 1...N { 6 if good(n, false) { count += 1 } 7 } 8 return count 9 } 10 11 func good(_ n : Int, _ flag: Bool) -> Bool { 12 if n == 0 { return flag } 13 let d = n%10 14 if d == 3 || d == 4 || d == 7 { return false } 15 if d == 0 || d == 1 || d == 8 { return good(n/10, flag) } 16 return good(n/10, true) 17 } 18 }
16ms
1 class Solution { 2 func rotatedDigits(_ N: Int) -> Int { 3 var count = 0 4 5 for numRaw in 0...N { 6 var containDifferent = false 7 var containInvalid = false 8 9 var num = numRaw 10 while num > 0 { 11 let dig = num % 10 12 num = num / 10 13 14 switch (dig) { 15 case 0,1,8: 16 break 17 case 2,5,6,9: 18 containDifferent = true 19 case 3,4,7: 20 containInvalid = true 21 default: 22 break; 23 } 24 25 if containInvalid { 26 break 27 } 28 } 29 30 if containDifferent && !containInvalid { 31 count += 1 32 } 33 } 34 35 return count 36 } 37 }
24ms
1 class Solution { 2 func rotatedDigits(_ N: Int) -> Int { 3 var dp = Array(repeating: 0, count: N + 1) 4 var goodNumCount = 0 5 6 for num in 0...N { 7 if num <= 9 { 8 if num == 2 || num == 5 || num == 6 || num == 9 { 9 dp[num] = 2 10 goodNumCount += 1 11 } else if num == 0 || num == 1 || num == 8 { 12 dp[num] = 1 13 } 14 } else { 15 let prevDigits = dp[num / 10] 16 let lastDigit = dp[num % 10] 17 18 if prevDigits == 1 && lastDigit == 1 { 19 dp[num] = 1 20 } else if prevDigits >= 1 && lastDigit >= 1 { 21 dp[num] = 2 22 goodNumCount += 1 23 } 24 } 25 } 26 27 return goodNumCount 28 } 29 }
28ms
1 class Solution { 2 var rotations = [0, 1, 5, -1, -1, 2, 9, -1, 8, 6] 3 4 func rotatedDigits(_ N: Int) -> Int { 5 guard N != 0 else { return 0 } 6 7 var count = 0 8 for i in 1...N { 9 count += isGood(i) ? 1 : 0 10 } 11 12 return count 13 } 14 15 func isGood(_ n: Int) -> Bool { 16 var newNr = 0 17 var multiply = 1 18 var workingN = n 19 var d = 0 20 21 while workingN != 0 { 22 d = workingN % 10 23 if rotations[d] == -1 { return false } 24 25 newNr = rotations[d] * multiply + newNr 26 workingN /= 10 27 multiply *= 10 28 } 29 30 return n != newNr 31 } 32 }
40ms
1 class Solution { 2 private let dict: [Int: Int] = [0: 0, 1: 1, 8: 8, 2: 5, 5: 2, 9: 6, 6: 9] 3 func rotatedDigits(_ N: Int) -> Int { 4 guard N > 1 else { return 0 } 5 var curNum = N 6 var changed: Bool = false 7 while curNum > 0 { 8 let digit = curNum % 10 9 let changedDigit = dict[digit] ?? -1 10 if changedDigit == -1 { 11 return rotatedDigits(N - 1) 12 } else if digit != changedDigit { 13 changed = true 14 } 15 curNum /= 10 16 } 17 return (changed ? 1 : 0) + rotatedDigits(N - 1) 18 } 19 }
56ms
1 class Solution { 2 func rotatedDigits(_ N: Int) -> Int { 3 var c : Set<Int> = [2,5,6,9] 4 var k : Set<Int> = [0,1,8] 5 var ans = 0 6 for i in 1 ... N { 7 var j = i 8 var cn = 0, kn = 0 9 var valid = true 10 while j > 0 { 11 let d = j%10 12 if c.contains(d) { 13 cn += 1 14 } else if k.contains(d) { 15 kn += 1 16 } else { 17 valid = false 18 break 19 } 20 j = j/10 21 } 22 if valid && cn > 0 { 23 ans += 1 24 } 25 } 26 return ans 27 } 28 }
68ms
1 class Solution { 2 func rotatedDigits(_ N: Int) -> Int { 3 if N == 0 { 4 return 0 5 } 6 var count = 0 7 for i in 1...N { 8 if checkIfCanBeRotated(String(i)) { 9 count += 1 10 } 11 } 12 return count 13 } 14 15 func checkIfCanBeRotated(_ str: String) -> Bool { 16 var res = false 17 for s in str { 18 if s == "2" || s == "5" || s == "6" || s == "9" { 19 res = true 20 } else if s == "3" || s == "4" || s == "7" { 21 return false 22 } 23 } 24 return res 25 } 26 }
196ms
1 class Solution { 2 func rotatedDigits(_ N: Int) -> Int { 3 let invalid: Set<Character> = Set(["3", "4", "7"]) 4 let diff: Set<Character> = Set(["2", "5", "6", "9"]) 5 var res = 0 6 7 for i in 1...N { 8 let lookup = Set(Array(String(i))) 9 //确定两个集合是否没有共同的值 10 if lookup.isDisjoint(with: invalid) && !lookup.isDisjoint(with: diff) { 11 res += 1 12 } 13 } 14 15 return res 16 } 17 }
转载于:https://www.cnblogs.com/strengthen/p/10545646.html
[Swift]LeetCode788. 旋转数字 | Rotated Digits相关推荐
- 使用LeNet对于旋转数字进行识别:合并数字集合
简 介: 将所有机械旋转字符合成一个大的训练集合(3415个样本),使用其中80%作为训练样本集合,利用LeNet网络进行训练.最终在测试集合上获得95%的识别率.对于误差超过1的样本只要0.7%. ...
- [LeetCode788] Rotated Digits 旋转数字
一.题目 我们称一个数 X 为好数, 如果它的每位数字逐个地被旋转 180 度后,我们仍可以得到一个有效的,且和 X 不同的数.要求每位数字都要被旋转. 如果一个数的每位数字被旋转以后仍然还是一个数字 ...
- C#LeetCode刷题之#788-旋转数字(Rotated Digits)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3967 访问. 我们称一个数 X 为好数, 如果它的每位数字逐个地 ...
- 【788. 旋转数字】
来源:力扣(LeetCode) 描述: 我们称一个数 X 为好数, 如果它的每位数字逐个地被旋转 180 度后,我们仍可以得到一个有效的,且和 X 不同的数.要求每位数字都要被旋转. 如果一个数的每位 ...
- Leetcode 788: Rotated Digits
问题描述: x is a good number if after rotating each digit individually by 180 degrees, we get a valid nu ...
- LeetCode 788. 旋转数字
1. 题目 我们称一个数 X 为好数, 如果它的每位数字逐个地被旋转 180 度后,我们仍可以得到一个有效的,且和 X 不同的数.要求每位数字都要被旋转. 如果一个数的每位数字被旋转以后仍然还是一个数 ...
- LeetCode 788. Rotated Digits
题目: X is a good number if after rotating each digit individually by 180 degrees, we get a valid numb ...
- LeetCode(788)——旋转数字(JavaScript)
我们称一个数 X 为好数, 如果它的每位数字逐个地被旋转 180 度后,我们仍可以得到一个有效的,且和 X 不同的数.要求每位数字都要被旋转. 如果一个数的每位数字被旋转以后仍然还是一个数字, 则这个 ...
- 数字识别digits.png研究
这段时间一直在研究数字识别, 但在识别我自己的手写体上面,在首次实验中一直效果不佳.觉得从最开始研究. 本文主要研究digits.png的字体长宽关系 此处主要利用函数为Cv2.BoundingRec ...
最新文章
- Int,Long比较重使用equal替换==
- arm-eabi-gcc: error trying to exec 'cc1': execvp: No such file or directory
- ubuntu下python thrift安装
- 增加表空间大小的四种方法
- 【欧洲已死】李开复:欧洲人工智能毫无希望
- 二次优化问题dfp_MATLAB优化问题应用实例讲解
- Filtering 过滤操作
- Big Sur系统更新后,运行变慢?使macOS Big Sur更快运行的12个技巧
- ORACLE 正則表達式
- 计算机维修兴趣小组,计算机兴趣小组章程
- 有关结合律和优先规律
- OTN关联开销(电层开销)OTU开销ODU开销OPU开销
- 抖音短视频标题什么样的容易火:国仁楠哥
- 使用Python 绘制双Y轴和误差棒柱状图
- 文档习惯之格式篇 用文档逼疯一个人的五种办法
- PAT-2019年冬季考试-甲级-7-1 Good in C (20分)超详解,几招就满分通过
- Composing Programs 2.3 Sequence - 02
- ThinkPHP模版引擎之变量输出具体解释
- 基于Singer混沌映射的麻雀搜索算法-附代码
- 全球神秘失踪--多维世界或时空扭曲解谜
热门文章
- python数据分析 - 关联规则Apriori算法
- 超详细从零开始安装yolov5
- oracle erp和金蝶,什么是erp系统,集体和个人选择时有哪些不同
- 体外反搏做_放手一搏,谈谈体外反搏和体内反搏
- 最新版勤哲Excel服务器V2017.13.0.1无限用户支持手机APP,微信,任意安装,支持后续升级
- python k线顶分型_顶分型和底分型的确认及K线包含处理
- 软件编程需要记吗?(工作中切记,天外有天,人外有人,务必谦逊低调,谨言慎行,不卑不亢,有礼有节,戒急用忍,与大家共勉!)
- 上海 python 培训 兄弟连
- Camtasia Recorder
- c 程序设计语言 好难,C程序设计语言难点及分析