Skip to content

Commit 9fa3e35

Browse files
committed
一刷816
1 parent 13aeb0e commit 9fa3e35

6 files changed

Lines changed: 119 additions & 45 deletions

File tree

README.adoc

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5737,12 +5737,12 @@ TIP: **公众号的微信号是: `jikerizhi`**。__因为众所周知的原因
57375737
|Hard
57385738
|
57395739

5740-
//|{counter:codes}
5741-
//|{leetcode_base_url}/ambiguous-coordinates/[816. Ambiguous Coordinates^]
5742-
//|{source_base_url}/_0816_AmbiguousCoordinates.java[Java]
5743-
//|{doc_base_url}/0816-ambiguous-coordinates.adoc[题解]
5744-
//|Medium
5745-
//|
5740+
|{counter:codes}
5741+
|{leetcode_base_url}/ambiguous-coordinates/[816. Ambiguous Coordinates^]
5742+
|{source_base_url}/_0816_AmbiguousCoordinates.java[Java]
5743+
|{doc_base_url}/0816-ambiguous-coordinates.adoc[题解]
5744+
|Medium
5745+
|
57465746

57475747
|{counter:codes}
57485748
|{leetcode_base_url}/linked-list-components/[817. Linked List Components^]
Lines changed: 56 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,80 @@
11
[#0816-ambiguous-coordinates]
2-
= 816. Ambiguous Coordinates
2+
= 816. 模糊坐标
33

4-
{leetcode}/problems/ambiguous-coordinates/[LeetCode - Ambiguous Coordinates^]
4+
https://leetcode.cn/problems/ambiguous-coordinates/[LeetCode - 816. 模糊坐标^]
55

6-
We had some 2-dimensional coordinates, like `"(1, 3)"` or `"(2, 0.5)"`. Then, we removed all commas, decimal points, and spaces, and ended up with the string `S`. Return a list of strings representing all possibilities for what our original coordinates could have been.
6+
我们有一些二维坐标,如 `(1, 3)``(2, 0.5)`,然后我们移除所有逗号,小数点和空格,得到一个字符串`S`。返回所有可能的原始字符串到一个列表中。
77

8-
Our original representation never had extraneous zeroes, so we never started with numbers like "00", "0.0", "0.00", "1.0", "001", "00.01", or any other number that can be represented with less digits. Also, a decimal point within a number never occurs without at least one digit occuring before it, so we never started with numbers like ".1".
8+
原始的坐标表示法不会存在多余的零,所以不会出现类似于"00", "0.0", "0.00", "1.0", "001", "00.01"或一些其他更小的数来表示坐标。此外,一个小数点前至少存在一个数,所以也不会出现“.1”形式的数字。
99

10-
The final answer list can be returned in any order. Also note that all coordinates in the final answer have exactly one space between them (occurring after the comma.)
10+
最后返回的列表可以是任意顺序的。而且注意返回的两个数字中间(逗号之后)都有一个空格。
1111

12-
[subs="verbatim,quotes,macros"]
13-
----
14-
*Example 1:*
15-
*Input:* "(123)"
16-
*Output:* ["(1, 23)", "(12, 3)", "(1.2, 3)", "(1, 2.3)"]
17-
----
12+
....
13+
示例 1:
14+
输入: "(123)"
15+
输出: ["(1, 23)", "(12, 3)", "(1.2, 3)", "(1, 2.3)"]
16+
....
1817

19-
[subs="verbatim,quotes,macros"]
20-
----
21-
*Example 2:*
22-
*Input:* "(00011)"
23-
*Output:* ["(0.001, 1)", "(0, 0.011)"]
24-
*Explanation:*
25-
0.0, 00, 0001 or 00.01 are not allowed.
26-
----
18+
....
19+
示例 2:
20+
输入: "(00011)"
21+
输出: ["(0.001, 1)", "(0, 0.011)"]
22+
解释:
23+
0.0, 00, 0001 或 00.01 是不被允许的。
24+
....
2725

28-
[subs="verbatim,quotes,macros"]
29-
----
30-
*Example 3:*
31-
*Input:* "(0123)"
32-
*Output:* ["(0, 123)", "(0, 12.3)", "(0, 1.23)", "(0.1, 23)", "(0.1, 2.3)", "(0.12, 3)"]
33-
----
26+
....
27+
示例 3:
28+
输入: "(0123)"
29+
输出: ["(0, 123)", "(0, 12.3)", "(0, 1.23)", "(0.1, 23)", "(0.1, 2.3)", "(0.12, 3)"]
30+
....
3431

35-
[subs="verbatim,quotes,macros"]
36-
----
37-
*Example 4:*
38-
*Input:* "(100)"
39-
*Output:* [(10, 0)]
40-
*Explanation:*
41-
1.0 is not allowed.
42-
----
32+
....
33+
示例 4:
34+
输入: "(100)"
35+
输出: [(10, 0)]
36+
解释:
37+
1.0 是不被允许的。
38+
....
4339

44-
40+
*提示:*
4541

46-
*Note: *
42+
* `4 \<= S.length \<= 12`.
43+
* `S[0] = (`, `S[S.length - 1] = )`, 且字符串 `S` 中的其他元素都是数字。
4744
4845
49-
* `4 <= S.length <= 12`.
50-
* `S[0]` = "(", `S[S.length - 1]` = ")", and the other elements in `S` are digits.
5146
47+
== 思路分析
5248

53-
49+
将字符串进行分割,然后给每个分割的字符串添加小数点,要排除非法数字的情况。
5450

51+
image::images/0816-10.png[{image_attr}]
5552

5653

5754
[[src-0816]]
55+
[tabs]
56+
====
57+
一刷::
58+
+
59+
--
5860
[{java_src_attr}]
5961
----
6062
include::{sourcedir}/_0816_AmbiguousCoordinates.java[tag=answer]
6163
----
64+
--
65+
66+
// 二刷::
67+
// +
68+
// --
69+
// [{java_src_attr}]
70+
// ----
71+
// include::{sourcedir}/_0816_AmbiguousCoordinates_2.java[tag=answer]
72+
// ----
73+
// --
74+
====
75+
76+
77+
== 参考资料
6278

79+
. https://leetcode.cn/problems/ambiguous-coordinates/solutions/1951931/mo-hu-zuo-biao-by-leetcode-solution-y1yz/[816. 模糊坐标 - 官方题解^]
80+
. https://leetcode.cn/problems/ambiguous-coordinates/solutions/1953793/zhua-wa-mou-si-tu-jie-leetcode-by-muse-7-7y25/[816. 模糊坐标 - 图解LeetCode^]

docs/images/0816-10.png

354 KB
Loading

docs/index.adoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1724,7 +1724,7 @@ include::0814-binary-tree-pruning.adoc[leveloffset=+1]
17241724

17251725
include::0815-bus-routes.adoc[leveloffset=+1]
17261726

1727-
// include::0816-ambiguous-coordinates.adoc[leveloffset=+1]
1727+
include::0816-ambiguous-coordinates.adoc[leveloffset=+1]
17281728

17291729
include::0817-linked-list-components.adoc[leveloffset=+1]
17301730

logbook/202601.adoc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1001,6 +1001,11 @@ endif::[]
10011001
|{doc_base_url}/0817-linked-list-components.adoc[题解]
10021002
|✅ 链表。难得写得比官方题解更简洁。
10031003

1004+
|{counter:codes}
1005+
|{leetcode_base_url}/ambiguous-coordinates/[816. Ambiguous Coordinates^]
1006+
|{doc_base_url}/0816-ambiguous-coordinates.adoc[题解]
1007+
|✅ 枚举。字符串分割,然后添加小数点,排除非法数字。
1008+
10041009

10051010
|===
10061011

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package com.diguage.algo.leetcode;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
public class _0816_AmbiguousCoordinates {
7+
// tag::answer[]
8+
9+
/**
10+
* @author D瓜哥 · https://www.diguage.com
11+
* @since 2026-07-11 21:53:24
12+
*/
13+
public List<String> ambiguousCoordinates(String s) {
14+
s = s.substring(1, s.length() - 1);
15+
List<String> result = new ArrayList<>();
16+
for (int i = 1; i < s.length(); i++) {
17+
List<String> heads = toDecimal(s.substring(0, i));
18+
if (heads.isEmpty()) {
19+
continue;
20+
}
21+
List<String> tails = toDecimal(s.substring(i));
22+
if (tails.isEmpty()) {
23+
continue;
24+
}
25+
for (String h : heads) {
26+
for (String t : tails) {
27+
result.add('(' + h + ", " + t + ')');
28+
}
29+
}
30+
}
31+
return result;
32+
}
33+
34+
private List<String> toDecimal(String s) {
35+
List<String> result = new ArrayList<>();
36+
for (int i = 0; i < s.length(); i++) {
37+
String d = (i > 0 ? s.substring(0, i) + '.' : "") + s.substring(i);
38+
if ((d.charAt(0) == '0' && d.length() > 1 && d.charAt(1) != '.')
39+
|| (d.contains(".") && d.charAt(d.length() - 1) == '0')) {
40+
continue;
41+
}
42+
result.add(d);
43+
}
44+
return result;
45+
}
46+
47+
// end::answer[]
48+
static void main() {
49+
new _0816_AmbiguousCoordinates().ambiguousCoordinates("(123)");
50+
}
51+
}

0 commit comments

Comments
 (0)