在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★ We had some 2-dimensional coordinates, like 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:
我们有一些二维坐标,如 原始的坐标表示法不会存在多余的零,所以不会出现类似于"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 是不被允许的。 提示:
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 }
|
请发表评论