forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
_816.java
47 lines (44 loc) · 1.74 KB
/
_816.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package com.fishercoder.solutions;
import java.util.ArrayList;
import java.util.List;
public class _816 {
public static class Solution1 {
public List<String> ambiguousCoordinates(String s) {
s = s.substring(1, s.length() - 1);
List<String> list = new ArrayList<>();
for (int i = 1; i < s.length(); i++) {
String x = s.substring(0, i);
List<String> xs = findAllPossibilities(x);
String y = s.substring(i);
List<String> ys = findAllPossibilities(y);
for (String j : xs) {
for (String k : ys) {
list.add("(" + j + ", " + k + ")");
}
}
}
return list;
}
private List<String> findAllPossibilities(String str) {
List<String> result = new ArrayList<>();
if (str.length() == 1) {
result.add(str);
return result;
} else {
for (int i = 1; i < str.length(); i++) {
String integerPart = str.substring(0, i);
String floatPart = str.substring(i);
if (integerPart.length() > 1 && integerPart.charAt(0) != '0' && floatPart.charAt(floatPart.length() - 1) != '0') {
result.add(integerPart + "." + floatPart);
} else if (integerPart.length() == 1 && floatPart.charAt(floatPart.length() - 1) != '0') {
result.add(integerPart + "." + floatPart);
}
}
if (str.charAt(0) != '0') {
result.add(str);
}
}
return result;
}
}
}