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.

这道题给了我们一个模糊坐标,括号里面很只有一个数字字符串,没有逗号也没有小数点,让我们自己添加逗号和小数点,让把所有可能的组合都返回。题目中给了很多例子,理解起题意来很容易。这道题的难点是如何合理的拆分,很多拆分是不合法的,题目举了很多不合法的例子,比如 "00", "0.0", "0.00", "1.0", "001", "00.01"。那么我们需要归纳出所有不合法的corner case,然后剩下一般情况比如123,我们就按位加小数点即可。那么我们再来看一下那些非法的例子,我们发现一眼望去好多0,不错,0就是trouble maker,首先不能有0开头的长度大于1的整数,比如00, 001。其次,不能有0结尾的小数,比如0.0,0.00,1.0等。还有,小数的整数位上也不能有0开头的长度大于1的整数。那么我们来归纳一下吧,首先如果字符串为空,那么直接返回空集合。然后如果字符串长度大于1,且首尾字符都是0的话,那么不可分,比如 0xxx0,因为整数长度大于1的话不能以0开头,中间也没法加小数点,因为小数最后一位不能是0。如果长度大于1,第一位是0,但最后一位不是0,那我们可以在第一个0后面加个小数点返回,这时就必须要加小数点了,因为长度大于1的整数不能以0开头。再之后,如果最后一位是0,说明不能加小数点,直接把当前值返回即可。最后就是一般情况了,我们先把原数加入结果res,然后遍历中间的每个位置,都加个小数点,所有情况归纳如下:

if S == "": return []
if S == "0": return [S]
if S == "0XXX0": return []
if S == "0XXX": return ["0.XXX"]
if S == "XXX0": return [S]
return [S, "X.XXX", "XX.XX", "XXX.X"...]

class Solution {
public:
vector<string> ambiguousCoordinates(string S) {
vector<string> res;
int n = S.size();
for (int i = ; i < n - ; ++i) {
vector<string> A = findAll(S.substr(, i)), B = findAll(S.substr(i + , n - - i));
for (auto &a : A) {
for (auto &b : B) {
res.push_back("(" + a + ", " + b + ")");
}
}
}
return res;
}
vector<string> findAll(string S) {
int n = S.size();
if (n == || (n > && S[] == '' && S[n - ] == '')) return {};
if (n > && S[] == '') return {"0." + S.substr()};
if (S[n - ] == '') return {S};
vector<string> res{S};
for (int i = ; i < n; ++i) res.push_back(S.substr(, i) + "." + S.substr(i));
return res;
}
};

参考资料:

https://leetcode.com/problems/ambiguous-coordinates/

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

LeetCode All in One 题目讲解汇总(持续更新中...)

[LeetCode] Ambiguous Coordinates 模糊的坐标的更多相关文章

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

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

  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. 816. Ambiguous Coordinates

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

  5. React-router4 第九篇 Ambiguous Matches 模糊匹配

    https://reacttraining.com/react-router/web/example/ambiguous-matches 看了官方的例子,我准备把阮一峰老师的代码再粘贴一次..!!

  6. 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 ...

  7. Swift LeetCode 目录 | Catalog

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

  8. All LeetCode Questions List 题目汇总

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

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

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

随机推荐

  1. mysql递归

    sql Server可以用with as 语法,mysql没有这个功能,只能用别的方式了,目前的mysql版本中并不支持直接的递归查询,但是通过递归到迭代转化的思路,还是可以在一句SQL内实现树的递归 ...

  2. 无连接运输的UDP、可靠数据传输原理、面向连接运输的TCP

    由[RFC 768]定义的UDP只是做了运输协议能够做的最少工作.除了复用/分解功能极少量的差错检测外,它几乎没有对IP增加别的东西.如果应用程序开发人员选择UDP而不是TCP,则该应用程序差不多就是 ...

  3. JAVA IO练习

     停车场有进场和出场的功能1. 进场时:采用键盘录入的方式,录入汽车的品牌.颜色.车牌号. 把品牌.颜色.车牌号,以及进场时间写入car.txt文件中. 2. 出场时:键盘录入车牌号,去文件中查找该车 ...

  4. RoIPooling

    . 代码: template <typename Dtype> void ROIPoolingLayer<Dtype>::Forward_cpu(const vector< ...

  5. linux内存 free命令 buffer cache作用

    free命令用于查看linux内存使用情况 #free shared:用于进程之间相互共享数据. Used:已使用内存. total:内存总量. free:未使用的内存. available:开启一个 ...

  6. AB PLC分类

    AB PLC产品更新速度还是挺快的,以前很多产品都停产了,所以分类也跟着调整,就目前而言: 一.主要类型 AB PLC按类型,主要分为三大类: • 小型:MicroLogix控制器 • 中型:Comp ...

  7. C语言malloc函数为一维,二维,三维数组分配空间

    c语言允许建立内存动态分配区域,以存放一些临时用的数据,这些数据不必在程序的声明部分定义,也不必等到函数结束时才释放,而是需要时随时开辟,不需要时随时释放,这些数据存储在堆区.可以根据需要,向系统申请 ...

  8. 使用Elasticsearch-dump迁移ES数据

    1. Elasticsearch-dump 安装 1) yum install epel-release 2) yum install nodejs 3) yum install nodejs npm ...

  9. MySQL基础使用

    数据库 其实我们常常说的数据库,应该叫数据库系统. 表和库 数据表:用来保存数据的表格 数据库:用来统一管理数据表的容器 启动mysql 关闭mysql service mysqld start(启动 ...

  10. form表单获取多选的值

    flask 中 form 表单直接获取多选框的值时 language = request.values.getlist('values')或 language=request.from.getlist ...