816. Ambiguous Coordinates
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 stringS
. 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 inS
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:
816. Ambiguous Coordinates的更多相关文章
- 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 ...
- 【LeetCode】816. Ambiguous Coordinates 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.me/ 题目地址:https://leetcode.com/problems/ambiguous ...
- 【leetcode】816. Ambiguous Coordinates
题目如下: 解题思路:我的方案是先把S拆分成整数对,例如S='1230',先拆分成(1,230),(12,30),(123,0),然后再对前面整数对进行加小数点处理.比如(12,30)中的12可以加上 ...
- [Swift]LeetCode816. 模糊坐标 | Ambiguous Coordinates
We had some 2-dimensional coordinates, like "(1, 3)" or "(2, 0.5)". Then, we re ...
- [LeetCode] Ambiguous Coordinates 模糊的坐标
We had some 2-dimensional coordinates, like "(1, 3)" or "(2, 0.5)". Then, we re ...
- All LeetCode Questions List 题目汇总
All LeetCode Questions List(Part of Answers, still updating) 题目汇总及部分答案(持续更新中) Leetcode problems clas ...
- Swift LeetCode 目录 | Catalog
请点击页面左上角 -> Fork me on Github 或直接访问本项目Github地址:LeetCode Solution by Swift 说明:题目中含有$符号则为付费题目. 如 ...
- 【LeetCode】字符串 string(共112题)
[3]Longest Substring Without Repeating Characters (2019年1月22日,复习) [5]Longest Palindromic Substring ( ...
- Qt5.3编译错误——call of overloaded ‘max(int int)’is ambiguous
错误描述: 今天在使用Qt写一个C++函数模板的测试程序的时候,编译的时候,编译的时候出现如下错误: 错误描述为:在main函数中,进行函数max()重载时,出现(ambiguous)含糊的,不明确的 ...
随机推荐
- nodejs前端接口与状态转换调试
和UI无关的逻辑用browser 调有时不太方便,配置 node 命令行调试环境方法如下: cnpm install @babel/core @babel/cli @babel/register @b ...
- 获取网页title(还有一坑未填)
def getTitle(self,url): #get title title = 'time out' try: self.res = requests.get(url,timeout=5) so ...
- vue slot 复用
话不投机半句多,直接上代码 有3步 第一步:创建渲染slot的组件 重要 第二步:为slot添加父组件数据(props) 重要 第三步:使用 第一步:创建渲染slot的组件 首选创建一个单文件组价,由 ...
- 5个数组Array方法: indexOf、filter、forEach、map、reduce使用实例
ES5中,一共有9个Array方法 Array.prototype.indexOf Array.prototype.lastIndexOf Array.prototype.every Array.pr ...
- windows下运行Eigen
参看:https://blog.csdn.net/zhujiahui622/article/details/50504678## 下载C++下的一个线性代数运算库Eigen并解压:http://eig ...
- cnblog测试
文字测试 图片测试
- oracle service name sid , 用户 和 表空间
oracle 的四个概念: 数据库: 就是一堆静态的数据文件.注意是静态的 instance 实例: 可以类比数据库连接. 实例就是为了操作数据库而开辟的进程和内存空间,有了这个实例你才能操作数据库. ...
- Clinet/Server在工作线程中刷新页面数据的方法
Worker线程不能修改UI线程的状态(比如文本框里面的内容).解决的办法是写一个用来更新文本框内容的函数,然后在Worker线程里面通过BeginInvoke来利用delegate调用这个函数更新文 ...
- Python3 使用pymysql链接MySQL数据库
1,pymysql安装 pip install pymysql 2,pymysql使用 import pymysql 3,实例查询 import pymysql #获取一个数据库链接 #格式 主机地址 ...
- 从BIRT报表文件中获取页面设置信息(页边距、纸张大小、输出方向)的方法
从BIRT报表文件中获取页面设置信息(页边距.纸张大小.输出方向)的方法 报表打印时,尤其是套打的报表,页面设置信息非常重要,比如页边距,纸张大小,输出方向等,而且每个报表的相关参数有可能不同 ...