LC.P816[模糊坐标]

方法一:枚举

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
class Solution {
String s;

public List<String> ambiguousCoordinates(String ss) {
s = ss.substring(1, ss.length() - 1);
int n = s.length();
List<String> ans = new ArrayList<>();
for (int i = 0; i < n - 1; ++i) {
for (String x : search(0, i)) {
for (String y : search(i + 1, n - 1)) {
ans.add(String.format("(%s, %s)", x, y));
}
}
}
return ans;
}

private List<String> search(int start, int end) {
List<String> ans = new ArrayList<>();
if (start == end || s.charAt(start) != '0') ans.add(s.substring(start, end + 1));
for (int i = start; i < end; ++i) { // 枚举小数点:在 i 后面追加小数点
String a = s.substring(start, i + 1), b = s.substring(i + 1, end + 1);
if (a.length() > 1 && a.charAt(0) == '0') continue;
if (b.charAt(b.length() - 1) == '0') continue;
ans.add(a + "." + b);
}
return ans;
}
}
  • 时间复杂度:$O(n^3)$
  • 空间复杂度:$O(n^3)$