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)含糊的,不明确的 ...
随机推荐
- java 编程英语单词,语句
记录一下java 编程工作学习中常用的英语汇总 in other words: 换句话说 dangle :悬挂 separated:分开的 distinct:明显的,独特的 actual :实际的 i ...
- vue学习笔记(nvm安装)
https://github.com/creationix/nvm https://github.com/coreybutler/nvm-windows 慕课网:https://www.imooc.c ...
- 查找linux下进程占用CPU过高的原因,以php-fpm为例
很多时候,线上服务器的进程在某时间段内长时间占用CPU过高,为了优化,我们需要找出原因. 1.找出占用CPU最高的10个进程 ps aux | sort -k3nr | head -n 10 或查看占 ...
- SQL 2008发布与订阅
网的教程很多,大都是不能成功,只有这一篇是成功的! https://www.cnblogs.com/DBArtist/p/5803271.html
- 探索未知种族之osg类生物---渲染遍历之裁剪三
前言 在osgUtil::CullVisitor,我们发现apply函数的重载中,有CullVisitor::apply(Group& node),CullVisitor::apply(Swi ...
- 怎么把微信里的文件发到QQ?
对于如何将微信里的文件发到QQ这个问题,首先要看是在手机中操作还是在电脑上操作,针对不同的发送方式逐一介绍如下: 一.从手机微信发送文件到QQ 1.在手机微信中找到需要发送的文件打开,点击右上角竖排的 ...
- spring boot 源码分析
说明:spring boot版本 2.0.6.RELEASE 思绪 首先,大家认识spring boot是从@SpringBootApplication注解和org.springframework.b ...
- memcache集群
实现memcache集群 一:memcache本身没有redis锁具备的数据持久化功能,比如RDB和AOF都没有,但是可以通过做集群的方式,让各memcache的数据进行同步,实现数据的一致性,即 ...
- keepalived高可用简介与配置
keepalived简介 keepalived介绍 Keepalived 软件起初是专为LVS负载均衡软件设计的,用来管理并监控LVS集群系统中各个服务节点的状态,后来又加入了可以实现高可用的VRRP ...
- 用pandas库修改excel文件里的内容,并把excel文件格式存为csv格式,再将csv格式改为html格式
假设有Excel文件data.xlsx,其中内容为: ID age height sex weight张三 1 39 181 female 85李四 2 ...