[Swift]LeetCode788. 旋轉數字 | Rotated Digits

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公衆號:山青詠芝(shanqingyongzhi)
➤博客園地址:山青詠芝(https://www.cnblogs.com/strengthen/
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址: http://www.javashuo.com/article/p-mghmbmnt-me.html 
➤若是連接不是山青詠芝的博客園地址,則多是爬取做者的文章。
➤原文已修改更新!強烈建議點擊原文地址閱讀!支持做者!支持原創!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★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.git

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.github

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:spa

  • N  will be in range [1, 10000].

咱們稱一個數 X 爲好數, 若是它的每位數字逐個地被旋轉 180 度後,咱們仍能夠獲得一個有效的,且和 X 不一樣的數。要求每位數字都要被旋轉。rest

若是一個數的每位數字被旋轉之後仍然仍是一個數字, 則這個數是有效的。0, 1, 和 8 被旋轉後仍然是它們本身;2 和 5 能夠互相旋轉成對方;6 和 9 同理,除了這些之外其餘的數字旋轉之後都再也不是有效的數字。code

如今咱們有一個正整數 N, 計算從 1 到 N 中有多少個數 X 是好數?htm

示例:
輸入: 10
輸出: 4
解釋: 
在[1, 10]中有四個好數: 2, 5, 6, 9。
注意 1 和 10 不是好數, 由於他們在旋轉以後不變。

注意:blog

  • N 的取值範圍是 [1, 10000]

Runtime: 8 ms
Memory Usage: 18.8 MB
 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 }

12msip

 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 }
相關文章
相關標籤/搜索