[Swift]LeetCode816. 模糊坐标 | Ambiguous Coordinates

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(shanqingyongzhi)
➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址: http://www.javashuo.com/article/p-uuesnanx-me.html 
➤若是连接不是山青咏芝的博客园地址,则多是爬取做者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持做者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★html

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

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

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。返回全部可能的原始字符串到一个列表中。app

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

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

示例 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 是不被容许的。

提示:spa

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

Runtime: 64 mscode

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 }
相关文章
相关标签/搜索