zoukankan      html  css  js  c++  java
  • [Swift]LeetCode816. 模糊坐标 | Ambiguous Coordinates

    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
    ➤微信公众号:山青咏芝(shanqingyongzhi)
    ➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/
    ➤GitHub地址:https://github.com/strengthen/LeetCode
    ➤原文地址: https://www.cnblogs.com/strengthen/p/10564493.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.

    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 }
  • 相关阅读:
    CCF201503-1 图像旋转(100分)
    CCF201509-1 数列分段(100分)
    CCF201509-1 数列分段(100分)
    JSP---使用checkbox实现多项删除
    JS---checkbox实现全选
    JSP---jsp页面获取物理路径
    JSP---根据值让某一Radio处于选中状态
    JSP---Myeclipse8.5使用Sql server数据库
    JSP---JSP学习笔记
    VS---解决VS2008专业版试用90天限制的方法
  • 原文地址:https://www.cnblogs.com/strengthen/p/10564493.html
Copyright © 2011-2022 走看看