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

[Swift]LeetCode816.模糊坐标|AmbiguousCoordinates

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

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

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

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

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

We had some 2-dimensional coordinates, like "(1, 3)" or "(2, 0.5)".  Then, we removed all commas, decimal points, and spaces, and ended up with the string S.  Return a list of strings representing all possibilities for what our original coordinates could have been.

Our original representation never had extraneous zeroes, so we never started with numbers like "00", "0.0", "0.00", "1.0", "001", "00.01", or any other number that can be represented with less digits.  Also, a decimal point within a number never occurs without at least one digit occuring before it, so we never started with numbers like ".1".

The final answer list can be returned in any order.  Also note that all coordinates in the final answer have exactly one space between them (occurring after the comma.)

Example 1:
Input: "(123)"
Output: ["(1, 23)", "(12, 3)", "(1.2, 3)", "(1, 2.3)"]
Example 2:
Input: "(00011)"
Output:  ["(0.001, 1)", "(0, 0.011)"]
Explanation: 
0.0, 00, 0001 or 00.01 are not allowed.
Example 3:
Input: "(0123)"
Output: ["(0, 123)", "(0, 12.3)", "(0, 1.23)", "(0.1, 23)", "(0.1, 2.3)", "(0.12, 3)"]
Example 4:
Input: "(100)"
Output: [(10, 0)]
Explanation: 
1.0 is not allowed. 

Note:

  • 4 <= S.length <= 12.
  • S[0] = "(", S[S.length - 1] = ")", and the other elements in S are digits.

我们有一些二维坐标,如 "(1, 3)" 或 "(2, 0.5)",然后我们移除所有逗号,小数点和空格,得到一个字符串S。返回所有可能的原始字符串到一个列表中。

原始的坐标表示法不会存在多余的零,所以不会出现类似于"00", "0.0", "0.00", "1.0", "001", "00.01"或一些其他更小的数来表示坐标。此外,一个小数点前至少存在一个数,所以也不会出现“.1”形式的数字。

最后返回的列表可以是任意顺序的。而且注意返回的两个数字中间(逗号之后)都有一个空格。

示例 1:
输入: "(123)"
输出: ["(1, 23)", "(12, 3)", "(1.2, 3)", "(1, 2.3)"]
示例 2:
输入: "(00011)"
输出:  ["(0.001, 1)", "(0, 0.011)"]
解释: 
0.0, 00, 0001 或 00.01 是不被允许的。
示例 3:
输入: "(0123)"
输出: ["(0, 123)", "(0, 12.3)", "(0, 1.23)", "(0.1, 23)", "(0.1, 2.3)", "(0.12, 3)"]
示例 4:
输入: "(100)"
输出: [(10, 0)]
解释: 
1.0 是不被允许的。

提示:

  • 4 <= S.length <= 12.
  • S[0] = "(", S[S.length - 1] = ")", 且字符串 S 中的其他元素都是数字。

Runtime: 64 ms

Memory Usage: 19.5 MB
 1 class Solution {
 2     func ambiguousCoordinates(_ S: String) -> [String] {
 3         var res:[String] = [String]()
 4         var n:Int = S.count
 5         for i in 1..<(n - 2)
 6         {
 7             var A:[String] = findAll(S.subString(1, i))
 8             var B:[String] = findAll(S.subString(i + 1, n - 2 - i))
 9             
10             for a in A
11             {
12                 for b in B
13                 {
14                     res.append("(" + a + ", " + b + ")")
15                 }
16             }
17         }
18         return res
19     }
20     
21     func findAll(_ S:String) -> [String]
22     {
23         var n:Int = S.count
24         if n == 0 || (n > 1 && S[0] == "0" && S[n - 1] == "0")
25         {
26             return []
27         }
28         if n > 1 && S[0] == "0"
29         {
30             return["0." + S.subString(1)]
31         }
32         if S[n - 1] == "0"
33         {
34             return [S]
35         }
36         var res:[String] = [S]
37         for i in 1..<n
38         {
39             res.append(S.subString(0, i) + "." + S.subString(i))
40         }
41         return res
42     }
43 }
44     
45 //String扩展
46 extension String {        
47     //subscript函数可以检索数组中的值
48     //直接按照索引方式截取指定索引的字符
49     subscript (_ i: Int) -> Character {
50         //读取字符
51         get {return self[index(startIndex, offsetBy: i)]}
52     }
53     
54     // 截取字符串:指定索引和字符数
55     // - begin: 开始截取处索引
56     // - count: 截取的字符数量
57     func subString(_ begin:Int,_ count:Int) -> String {
58         let start = self.index(self.startIndex, offsetBy: max(0, begin))
59         let end = self.index(self.startIndex, offsetBy:  min(self.count, begin + count))
60         return String(self[start..<end]) 
61     }
62     
63     // 截取字符串:从index到结束处
64     // - Parameter index: 开始索引
65     // - Returns: 子字符串
66     func subString(_ index: Int) -> String {
67         let theIndex = self.index(self.endIndex, offsetBy: index - self.count)
68         return String(self[theIndex..<endIndex])
69     }
70 }

 


鲜花

握手

雷人

路过

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

请发表评论

全部评论

专题导读
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

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

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

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