★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公衆號:山青詠芝(shanqingyongzhi)
➤博客園地址:山青詠芝(https://www.cnblogs.com/strengthen/)
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:http://www.javashuo.com/article/p-zhwggxne-mb.html
➤若是連接不是山青詠芝的博客園地址,則多是爬取做者的文章。
➤原文已修改更新!強烈建議點擊原文地址閱讀!支持做者!支持原創!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★html
A strobogrammatic number is a number that looks the same when rotated 180 degrees (looked at upside down).git
Write a function to count the total strobogrammatic numbers that exist in the range of low <= num <= high.github
Example:數組
Input: low = "50", high = "100" Output: 3 Explanation: 69, 88, and 96 are three strobogrammatic numbers.
Note:
Because the range might be a large number, the lowand high numbers are represented as string.微信
strobogramatic數字是旋轉180度時看起來相同的數字(上下顛倒)。ide
編寫一個函數來計算在low<=num<=high範圍內存在的總strobogrammatic數。函數
例子:spa
輸入:low=「50」,high=「100」code
輸出:3htm
說明:6九、88和96是三個strobogramatic數字。
注:
由於範圍多是一個大數字,因此低數字和高數字表示爲字符串。
1 class Solution { 2 func strobogrammaticInRange(_ low:String,_ high:String) -> Int { 3 var res:Int = 0 4 find(low, high, "", &res) 5 find(low, high, "0", &res) 6 find(low, high, "1", &res) 7 find(low, high, "8", &res) 8 return res; 9 } 10 11 func find (_ low:String,_ high:String,_ w:String,_ res:inout Int) 12 { 13 if w.count >= low.count && w.count <= high.count 14 { 15 if (w.count == low.count && w < low) || (w.count == high.count && w > high) 16 { 17 return 18 } 19 if !(w.count > 1 && w[0] == "0") 20 { 21 res += 1 22 } 23 } 24 if w.count + 2 > high.count 25 { 26 return 27 } 28 find(low, high, "0" + w + "0", &res) 29 find(low, high, "1" + w + "1", &res) 30 find(low, high, "6" + w + "9", &res) 31 find(low, high, "8" + w + "8", &res) 32 find(low, high, "9" + w + "6", &res) 33 } 34 } 35 36 extension String { 37 //subscript函數能夠檢索數組中的值 38 //直接按照索引方式截取指定索引的字符 39 subscript (_ i: Int) -> Character { 40 //讀取字符 41 get {return self[index(startIndex, offsetBy: i)]} 42 } 43 }