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 ...
随机推荐
- Day_14【IO流】扩展案例3_对文本文件中的字符串内容进行反转
分析以下需求,并用代码实现 项目根路径下有text.txt文件,内容如下 我爱黑马 123456 利用IO流的知识读取text.txt文件的内容反转后写入text.txt文件中 654321 马黑爱我 ...
- Excel开始,Excel结束,R语言居中
入职.离职,总公司调往分公司,分公司调往总公司,每月社保.公积金和上月比较有增减.税局导出的为Excel文件,需要和记录对照一番. 用Excel处理,那就是姓名粘贴为两列,条件格式-重复值,没变色的为 ...
- JAVA实现拼手气红包算法
实现拼手气红包算法,有以下几个需要注意的地方: 抢红包的期望收益应与先后顺序无关 保证每个用户至少能抢到一个预设的最小金额,人民币红包设置的最小金额一般是0.01元,如果需要发其他货币类型的红包,比如 ...
- 上位机开发之三菱Q系列PLC通信实践
经常关注我们公众号或者公开课的学员(如果还没有关注的话,左上角点击一波关注)应该知道,我们会经常使用西门子PLC,其实对于其他品牌的PLC,我们都会讲到,包括三菱.欧姆龙.基恩士.松下及国产台达.信捷 ...
- mybatis 新增返回id
第一种方式: 在实体类的映射文件 "*Mapper.xml" 这样写: <insert id="insertAndGetId" useGeneratedK ...
- [tgpl]go匿名函数
[tgpl]go匿名函数 0. 定义 匿名函数顾名思义是没有名字的函数, Named functions can be declared only at the package level, but ...
- .gitignore 模式匹配
匹配模式前使用 / 表示根目录 匹配模式后使用 / 代表是目录(不是文件) 匹配模式前加 ! 表示取反 * 代表任意个字符 ? 匹配任意一个字符 ** 匹配任意级目录
- js 前端向服务器端传送文件的常用请求方式
在做项目的过程当中写到文件上传的功能,想着之前也是踩坑过来的,就在这里总结下自己常用的方法吧.我们现在一般都是通过ajax来搭起前后端数据交互的桥梁,但是大家在做到有文件需要上传的时候就会发现我们用a ...
- css概述二
四.尺寸和边框 1.尺寸属性 ①作用 设置元素的宽度和高度 ②属性 width:宽度 max-width:最大宽度 min-width:最小宽度 height:高度 max-height: min-h ...
- Java开发架构篇:领域驱动设计架构基于SpringCloud搭建微服务
作者:小傅哥 博客:https://bugstack.cn 沉淀.分享.成长,让自己和他人都能有所收获! 一.前言介绍 微服务不是泥球小单体,而是具备更加清晰职责边界的完整一体的业务功能服务.领域驱动 ...