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 ...
随机推荐
- D365,实现批量上传和下载文件的工具
这里演示下批量上传文件到D365的小程序工具,下载功能也是一样的思路跟逻辑. 通过文件名的前缀跟各个主档表的主键进行绑定来决定将附件挂在哪里. 1.上传界面. 2.查看附件上传结果.
- 【Hadoop离线基础总结】MapReduce案例之自定义groupingComparator
MapReduce案例之自定义groupingComparator 求取Top 1的数据 需求 求出每一个订单中成交金额最大的一笔交易 订单id 商品id 成交金额 Order_0000005 Pdt ...
- VS2019 使用
下载 官网下载:链接 安装 1.点击下载程序,会显示这个界面 2.点击“继续”,等待安装程序安装完成 4.安装程序下载安装验证完毕,将会提示进入这个界面 5.为了方便起见,这里仅展示安装C++功能,在 ...
- 2018-06-30 js事件
一.js代码加载的时机 1.DOM加载完毕 -> 将js代码放到body体之下即可: 2.网页资源加载完毕-> $(window).onload(function(){ }); 3.jQ ...
- 前端面试题-TCP和UDP的区别
TCP和UDP的区别 (1)TCP是面向连接的,udp是无连接的即发送数据前不需要先建立链接. (2)TCP提供可靠的服务.也就是说,通过TCP连接传送的数据,无差错,不丢失,不重复,且按序到达;UD ...
- 解决 es CircuitBreakingException 问题
比如频繁报如下错误, [2019-06-16T15:31:22,778][DEBUG][o.e.a.a.c.n.i.TransportNodesInfoAction] [node-xxx] faile ...
- 感觉shopex现在的升级方式太慢了
我是说产品的更新,484,485是一个经典的版本,那时候免费,shopex 系统市场占用率很高.但是485以后呢,只有小版本的更新,fxw ,ekd 都是改进版本吧,没用特别大幅度的更新.5年前,10 ...
- .net core kafka 入门实例 一篇看懂
kafka 相信都有听说过,不管有没有用过,在江湖上可以说是大名鼎鼎,就像天龙八部里的乔峰.国际惯例,先介绍生平事迹 简介 Kafka 是由 Apache软件基金会 开发的一个开源流处理平台, ...
- 初涉WebGL
之前一直在捣鼓Vue和React栈,对组件化架构项目有了些理解和体会.今天尝尝WebGL,当然,并不打算现在深入,只是略作了解,我知道这个坑很深. js的图形库.3d库也有好几款比较流行的,如游戏开发 ...
- Codeforces1157A(A题)Reachable Numbers
A. Reachable Numbers Let's denote a function f(x)f(x) in such a way: we add 11 to xx, then, while th ...