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