Java实现 LeetCode 816 模糊坐标(暴力)
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 模糊坐标(暴力)的更多相关文章
- 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 ...
- Java for LeetCode 044 Wildcard Matching
Implement wildcard pattern matching with support for '?' and '*'. '?' Matches any single character. ...
- java使用elasticsearch进行模糊查询-已在项目中实际应用
java使用elasticsearch进行模糊查询 使用环境上篇文章本人已书写过,需要maven坐标,ES连接工具类的请看上一篇文章,以下是内容是笔者在真实项目中运用总结而产生,并写的是主要方法和思路 ...
- 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 ...
- 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. ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
随机推荐
- 5G新基建到来,图扑推出智慧路灯三维可视化方案
前言 作为智慧城市的重要组成部分,智慧灯杆管理系统采用信息化.数字化手段,把路灯及城市景观照明等各种不同对象的监控和数据采集及处理融于一体, 为城市管理者进行城市管理.进行科学决策提供了强有力的手段. ...
- Linux内核驱动学习(四)Platform设备驱动模型
Linux platform设备驱动模型 文章目录 Linux platform设备驱动模型 前言 框架 设备与驱动的分离 设备(device) 驱动(driver) 匹配(match) 参考 前言 ...
- HDU-6351 Beautiful Now 全排列暴力
Beautiful Now 题意 给出一个最大为10^9的数字n,以及一个k,你最多交换n中任意两个位置的数字k次,问形成的最大数字和最小数字. 思路 看到这题,我靠这题暴力交换一下,不难啊,咋没人做 ...
- 3D三栅极晶体管(摘抄)
英特尔的科学家们在2002年发明了三栅极晶体管——这是根据栅极有三面而取名的. 传统“扁平的”2D平面栅极被超级纤薄的.从硅基体垂直竖起的3D硅鳍状物所代替.电流控制是通过在鳍状物三面的每一面安装一个 ...
- flink基础篇
Flink面试--核心概念和基础考察 1.简单介绍一下 Flink 2.Flink 相比传统的 Spark Streaming 有什么区别? 3.Flink 的组件栈有哪些? 面试知识 ...
- python 基础应用3
1.使用while .for循环分别打印字符串s = 'fsufhjshh3hf'中每一个元素. #使用while .for循环分别打印字符串s = 'fsufhjshh3hf'中每一个元素. s = ...
- 【转载】文件上传那些事儿,文件ajax无刷上传
导语 正好新人导师让我看看能否把产品目前使用的FileUploader从老的组件库分离出来的,自己也查阅了相关的各种资料,对文件上传的这些事有了更进一步的了解.把这些知识点总结一下,供自己日后回顾,也 ...
- IDEA快捷键(windows)
Ctrl+Shift + Enter,语句完成“!”,否定完成,输入表达式时按 “!”键Ctrl+E,最近的文件Ctrl+Shift+E,最近更改的文件Shift+Click,可以关闭文件Ctrl+[ ...
- LUNIX命令集
Linux 是一套免费使用和自由传播的类 Unix 操作系统,是一个基于 POSIX 和 UNIX 的多用户.多任务.支持多线程和多 CPU 的操作系统. Linux 能运行主要的 UNIX 工具软件 ...
- nginx均衡负载
一直在担心session 问题,结果试了2个web 论坛,discuz 和phpbb ,前面用nginx 均衡负载,后端是apache httpd +php ,mysql 用同一个,修改一下confi ...