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<String> ambiguousCoordinates(String S) {
List<String> 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<String> l1 = split(perfix);
List<String> 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<String> split(StringBuilder sb) {
List<String> 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<String> ambiguousCoordinates(String S) {
List<String> ans = new ArrayList<>();
int n = S.length();
for (int i = 1; i < n - 1; ++i) {
List<String> 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<String> f(String s) {
int n = s.length();
List<String> 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:

https://leetcode.com/problems/ambiguous-coordinates/discuss/123851/C%2B%2BJavaPython-Solution-with-Explanation

816. Ambiguous Coordinates的更多相关文章

  1. 816 Ambiguous Coordinates (many cases problem)

    https://www.cnblogs.com/Java3y/p/8846955.html -- link of the problem 816 IDEA: check the dot and int ...

  2. 【LeetCode】816. Ambiguous Coordinates 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.me/ 题目地址:https://leetcode.com/problems/ambiguous ...

  3. 【leetcode】816. Ambiguous Coordinates

    题目如下: 解题思路:我的方案是先把S拆分成整数对,例如S='1230',先拆分成(1,230),(12,30),(123,0),然后再对前面整数对进行加小数点处理.比如(12,30)中的12可以加上 ...

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

    We had some 2-dimensional coordinates, like "(1, 3)" or "(2, 0.5)".  Then, we re ...

  5. [LeetCode] Ambiguous Coordinates 模糊的坐标

    We had some 2-dimensional coordinates, like "(1, 3)" or "(2, 0.5)".  Then, we re ...

  6. All LeetCode Questions List 题目汇总

    All LeetCode Questions List(Part of Answers, still updating) 题目汇总及部分答案(持续更新中) Leetcode problems clas ...

  7. Swift LeetCode 目录 | Catalog

    请点击页面左上角 -> Fork me on Github 或直接访问本项目Github地址:LeetCode Solution by Swift    说明:题目中含有$符号则为付费题目. 如 ...

  8. 【LeetCode】字符串 string(共112题)

    [3]Longest Substring Without Repeating Characters (2019年1月22日,复习) [5]Longest Palindromic Substring ( ...

  9. Qt5.3编译错误——call of overloaded ‘max(int int)’is ambiguous

    错误描述: 今天在使用Qt写一个C++函数模板的测试程序的时候,编译的时候,编译的时候出现如下错误: 错误描述为:在main函数中,进行函数max()重载时,出现(ambiguous)含糊的,不明确的 ...

随机推荐

  1. Populate screen data automatically

    field zz_test-uname. module populate_record on chain-request. module populate_record input. ztlo_tes ...

  2. vue iview render里面写时间截取

    render: (h, params) => {params.row.execDate = (params.row.execDate ? params.row.execDate.substr(0 ...

  3. angular 键盘事件绑定与过滤

    方便的angular按钮绑定 如 <input (keyup.enter)="keyUpSearch($event)" value="按下回车键触发"&g ...

  4. break 和continue在循环中起到的作用

    break语句的作用是终止当前循环,跳出循环体.主意,break只能跳出一层循环. continue语句的作用是终止本轮循环并开始下一轮循环,(这里要主意的是在开始下一轮循环之前,会先测试循环条件). ...

  5. windows7 ORA-12514 TNS 监听程序当前无法识别连接描述符中请求服务 的解决方法

    用PL/SQL连接虚拟机中的Oracle数据库,发现又报了“ORA-12514 TNS 监听程序当前无法识别连接描述符中请求服务”错误,帮其解决后,发现很多人遇到过这样的问题,因此写着这里. 也许你没 ...

  6. css伪类及伪元素用法

    注:该表引自W3School教程 伪元素的分类及作用: 接下来让博主通过一些生动的实例(之前的作业或小作品)来说明几种常用伪类的用法和效果,其他的读者可以自己尝试: :active  大致效果为用鼠标 ...

  7. 《笨方法学Python》加分题17

    题目通过前学习的文件操作把一个文件中的内容拷贝到另一个文件中,并使用 os.path.exists 在拷贝前判断被拷贝的文件是否已经存在,之后由用户判断是否继续完成拷贝. 新知识os.path.exi ...

  8. vue项目中编写一个图片预览的公用组件

    今天产品提出了一个查看影像的功能需求. 在查看单据的列表中,有一列是影像字段,一开始根据单据号调用接口查看是否有图片附件,如果有则弹出一个全屏的弹出层,如果没有给出提示.而且,从列表进入详情之后,附件 ...

  9. NC 部署问题

    1.was环境部署日志  IBM/WEBSPHERE/APPSERVER/PRORFILES/APPSRV01/LOGS/SERVER1/ 

  10. ci框架nginx访问

    url:http://localhost:20082/index.php/welcome/index 问题:apache环境下可以访问,nginx环境下不可以