816. Ambiguous Coordinates
发布日期:2021-05-09 02:51:25 浏览次数:18 分类:博客文章

本文共 4285 字,大约阅读时间需要 14 分钟。

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.

 

Approach #1: Brute Force. [Java] [Memory Limit Exceeded]

class Solution {    public List
ambiguousCoordinates(String S) { List
ans = new ArrayList<>(); StringBuilder sb = new StringBuilder(S); for (int i = 1; i < S.length(); ++i) { StringBuilder perfix = new StringBuilder(sb.substring(0, i)); StringBuilder suffix = new StringBuilder(sb.substring(i)); if (valid(perfix) && valid(suffix)) { List
l1 = split(perfix); List
l2 = split(suffix); for (int j = 0; j < l1.size(); ++j) { for (int k = 0; k < l2.size(); ++k) { String temp = "(" + l1.get(j) + ", " + l2.get(k) + ")"; ans.add(temp); } } } } return ans; } public List
split(StringBuilder sb) { List
ret = new ArrayList<>(); if (sb.length() == 1) { ret.add(sb.toString()); return ret; } else if (sb.charAt(0) == '0' && sb.charAt(1) == '0') { sb.insert(1, '.'); ret.add(sb.toString()); return ret; } else if (sb.charAt(sb.length() - 1) == '0' && sb.charAt(sb.length() - 2) == '0') { sb.insert(sb.length() - 1, '.'); ret.add(sb.toString()); return ret; } else { for (int i = 1; i < sb.length() - 1; ++i) { StringBuilder temp = sb; sb.insert(i, '.'); ret.add(sb.toString()); } } return ret; } public boolean valid(StringBuilder sb) { if (sb.length() == 1) return true; if (sb.length() > 4 && sb.charAt(0) == '0' && sb.charAt(1) == 0 && sb.charAt(sb.length() - 2) == '0' && sb.charAt(sb.length() - 1) == '0') return false; for (int i = 0; i < sb.length(); ++i) if (sb.charAt(i) != '0') return true; return false; }}

������

Approach #2: String. [Java]

class Solution {    public List
ambiguousCoordinates(String S) { List
ans = new ArrayList<>(); int n = S.length(); for (int i = 1; i < n - 1; ++i) { List
A = f(S.substring(1, i)), B = f(S.substring(i, n-1)); for (String a : A) for (String b : B) ans.add("(" + a + ", " + b + ")"); } return ans; } public List
f(String s) { int n = s.length(); List
ret = new ArrayList<>(); if (n == 0 || n > 1 && s.charAt(0) == '0' && s.charAt(n-1) == '0') return ret; if (n > 1 && s.charAt(0) == '0') { ret.add("0." + s.substring(1)); return ret; } ret.add(s); if (n == 1 || s.charAt(n-1) == '0') return ret; for (int i = 1; i < n; ++i) { ret.add(s.substring(0, i) + "." + s.substring(i, n)); } return ret; }}

 

Analysis:

if S == "" : return []

if S == "0" : return [S]

if S == "0XXXX0" : return []

if S == "0XXXX" : return ["0.XXXX"]

if S == "XXXX0" : return [S]

return [S, "X.XXX", "XX.XX", "XXX.X" ...]

������

Reference:

 

上一篇:833. Find And Replace in String
下一篇:770. Basic Calculator IV

发表评论

最新留言

网站不错 人气很旺了 加油
[***.192.178.218]2025年05月03日 06时32分04秒