• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

[Swift]LeetCode248.对称数III$StrobogrammaticNumberIII

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(shanqingyongzhi)
➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:https://www.cnblogs.com/strengthen/p/10214629.html 
➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★

热烈欢迎,请直接点击!!!

进入博主App Store主页,下载使用各个作品!!!

注:博主将坚持每月上线一个新app!!!

A strobogrammatic number is a number that looks the same when rotated 180 degrees (looked at upside down).

Write a function to count the total strobogrammatic numbers that exist in the range of low <= num <= high.

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度时看起来相同的数字(上下颠倒)。

编写一个函数来计算在low<=num<=high范围内存在的总strobogrammatic数。

例子:

输入:low=“50”,high=“100”

输出:3

说明:69、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 }

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
OpenStackSwiftclient开发发布时间:2022-07-14
下一篇:
[Swift]LeetCode632.最小区间|SmallestRange发布时间:2022-07-14
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap