816. 模糊坐标

我们有一些二维坐标,如 “(1, 3)” 或 “(2, 0.5)”,然后我们移除所有逗号,小数点和空格,得到一个字符串S。返回所有可能的原始字符串到一个列表中。

原始的坐标表示法不会存在多余的零,所以不会出现类似于"00", “0.0”, “0.00”, “1.0”, “001”, "00.01"或一些其他更小的数来表示坐标。此外,一个小数点前至少存在一个数,所以也不会出现“.1”形式的数字。

最后返回的列表可以是任意顺序的。而且注意返回的两个数字中间(逗号之后)都有一个空格。

示例 1:

输入: “(123)”

输出: ["(1, 23)", “(12, 3)”, “(1.2, 3)”, “(1, 2.3)”]

示例 2:

输入: “(00011)”

输出: ["(0.001, 1)", “(0, 0.011)”]

解释:

0.0, 00, 0001 或 00.01 是不被允许的。

示例 3:

输入: “(0123)”

输出: ["(0, 123)", “(0, 12.3)”, “(0, 1.23)”, “(0.1, 23)”, “(0.1, 2.3)”, “(0.12, 3)”]

示例 4:

输入: “(100)”

输出: [(10, 0)]

解释:

1.0 是不被允许的。

提示:

4 <= S.length <= 12.

S[0] = “(”, S[S.length - 1] = “)”, 且字符串 S 中的其他元素都是数字。

PS:

左右分开的暴力解法

class Solution {
public List<String> ambiguousCoordinates(String S) {
int len = S.length();
S = S.substring(1, len - 1);
len -= 2;
List<String> res = new ArrayList<>();
for(int i = 0; i < len - 1; i++){
List<String> left = helper(S.substring(0, i + 1));
List<String> right = helper(S.substring(i + 1));
if(left.size() == 0 || right.size() == 0){
continue;
}
for(String l : left){
for(String r : right){
StringBuilder sb = new StringBuilder();
sb.append("(").append(l).append(", ").append(r).append(")");
res.add(sb.toString());
}
}
}
return res;
} private List<String> helper(String str){
List<String> list = new ArrayList<>();
int len = str.length();
if(str.charAt(len - 1) == '0'){
if(len == 1){
list.add("0");
}
else if(str.charAt(0) != '0'){
list.add(str);
}
return list;
}
if(str.charAt(0) == '0'){
list.add("0." + str.substring(1));
return list;
}
list.add(str);
for(int i = 0; i < len - 1; i++){
StringBuilder sb = new StringBuilder();
sb.append(str.substring(0, i + 1)).append('.').append(str.substring(i + 1));
list.add(sb.toString());
}
return list;
}
}

Java实现 LeetCode 816 模糊坐标(暴力)的更多相关文章

  1. Java for LeetCode 060 Permutation Sequence

    The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of the p ...

  2. Java for LeetCode 044 Wildcard Matching

    Implement wildcard pattern matching with support for '?' and '*'. '?' Matches any single character. ...

  3. java使用elasticsearch进行模糊查询-已在项目中实际应用

    java使用elasticsearch进行模糊查询 使用环境上篇文章本人已书写过,需要maven坐标,ES连接工具类的请看上一篇文章,以下是内容是笔者在真实项目中运用总结而产生,并写的是主要方法和思路 ...

  4. Java for LeetCode 216 Combination Sum III

    Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...

  5. Java for LeetCode 214 Shortest Palindrome

    Given a string S, you are allowed to convert it to a palindrome by adding characters in front of it. ...

  6. Java for LeetCode 212 Word Search II

    Given a 2D board and a list of words from the dictionary, find all words in the board. Each word mus ...

  7. Java for LeetCode 211 Add and Search Word - Data structure design

    Design a data structure that supports the following two operations: void addWord(word)bool search(wo ...

  8. Java for LeetCode 210 Course Schedule II

    There are a total of n courses you have to take, labeled from 0 to n - 1. Some courses may have prer ...

  9. Java for LeetCode 200 Number of Islands

    Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surro ...

随机推荐

  1. 王颖奇 201771010129 第三周 Java基本程序设计总结

    实验三 Java基本程序设计 实验时间 2018-9-13 201771010129 王颖奇 1.实验目的与要求 (1)进一步掌握Eclipse集成开发环境下java程序开发基本步骤: (2)熟悉PT ...

  2. 记录一下关于在工具类中更新UI使用RunOnUiThread犯的极其愚蠢的错误

    由于Android中不能在子线程中更新ui,所以平时在子线程中需要更新ui时可以使用Android提供的RunOnUiThread接口,但是最近在写联网工具类的时候,有时候会出现联网异常,这个时候为了 ...

  3. C# 中 枚举Enum 一些转换的方法整理

    工作中 经常遇到枚举 的一些转换  特别是获取枚举备注等  特地整理下 方法以后使用 public void TestMethod1() { TestEnumOne colorEnum = TestE ...

  4. 【HBase】表模型和基本操作介绍

    目录 HBase表模型 常用Shell操作 HBase表模型 创建一个hbase表,必须要有表名和列族名 列族 -- columnFamily,就是上图中的Column Family1 userInf ...

  5. vue-cli中浏览器图标的配置

    在VUE全家桶项目里面,这里给大家提供了2种方案,进行浏览器图标的配置. a):先把图片准备好,放在static文件夹下,再找到根目录下的index.html文件,并打开,在HTML文档的<he ...

  6. jmeter录制rabbitmq消息-性能测试

     一.目的 为了测试系统的稳定性,在UAT环境下,通一段时间内不间断发送MQ消息来验证系统是否会出现异常. 二.测试工具 使用测试工具:jmeter5.2.1,火狐浏览器71.0,RabbitMQ管理 ...

  7. [hdu4888]最大流,判断最大流唯一性

    题意:给一个n*m的矩形,往每个格子填0-k的数字,使得对第i行和为row[i],第i列和为col[i],问是否存在方案,方案是否唯一,如果方案唯一则输出具体方案. 思路:首先根据问题提取对象,行.列 ...

  8. 1008 Elevator (20分)

    1008 Elevator (20分) 题目: The highest building in our city has only one elevator. A request list is ma ...

  9. Kafka面试你不得不知道的基础知识

    Java内存管理面试指南一 Java基础面试指南一 Java基础面试指南二 Java基础面试指南三 Java基础面试指南四 Java线程面试指南一 Java线程面试指南二 Redis面试指南一 Kaf ...

  10. ClickHouse基本操作(二)

    一.先来说一下,ClickHouse为啥快 MySQL单条SQL是单线程的,只能跑满一个core,ClickHouse相反,有多少CPU,吃多少资源,所以飞快: ClickHouse不支持事务,不存在 ...