Skip to content

Commit 8dab753

Browse files
committed
一刷1496
1 parent 7899635 commit 8dab753

File tree

7 files changed

+78
-32
lines changed

7 files changed

+78
-32
lines changed

README.adoc

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10498,12 +10498,12 @@ TIP: **公众号的微信号是: `jikerizhi`**。__因为众所周知的原因
1049810498
//|Easy
1049910499
//|
1050010500

10501-
//|{counter:codes}
10502-
//|{leetcode_base_url}/path-crossing/[1496. Path Crossing^]
10503-
//|{source_base_url}/_1496_PathCrossing.java[Java]
10504-
//|{doc_base_url}/1496-path-crossing.adoc[题解]
10505-
//|Easy
10506-
//|
10501+
|{counter:codes}
10502+
|{leetcode_base_url}/path-crossing/[1496. Path Crossing^]
10503+
|{source_base_url}/_1496_PathCrossing.java[Java]
10504+
|{doc_base_url}/1496-path-crossing.adoc[题解]
10505+
|Easy
10506+
|
1050710507

1050810508
//|{counter:codes}
1050910509
//|{leetcode_base_url}/check-if-array-pairs-are-divisible-by-k/[1497. Check If Array Pairs Are Divisible by k^]

docs/1496-path-crossing.adoc

Lines changed: 28 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,47 @@
11
[#1496-path-crossing]
2-
= 1496. Path Crossing
2+
= 1496. 判断路径是否相交
33

4-
{leetcode}/problems/path-crossing/[LeetCode - 1496. Path Crossing ^]
4+
https://leetcode.cn/problems/path-crossing/[LeetCode - 1496. 判断路径是否相交 ^]
55

6-
Given a string `path`, where `path[i] = 'N'`, `'S'`, `'E'` or `'W'`, each representing moving one unit north, south, east, or west, respectively. You start at the origin `(0, 0)` on a 2D plane and walk on the path specified by `path`.
6+
给你一个字符串 `path`,其中 `path[i]` 的值可以是 `N``S``E` 或者 `W`,分别表示向北、向南、向东、向西移动一个单位。
77

8-
Return `true` _if the path crosses itself at any point, that is, if at any time you are on a location you have previously visited_. Return `false` otherwise.
8+
你从二维平面上的原点 `(0, 0)` 处开始出发,按 `path` 所指示的路径行走。
99

10-
11-
*Example 1:*
12-
<img alt="" src="https://assets.leetcode.com/uploads/2020/06/10/screen-shot-2020-06-10-at-123929-pm.png" style="width: 400px; height: 358px;" />
13-
[subs="verbatim,quotes"]
14-
----
15-
*Input:* path = "NES"
16-
*Output:* false
17-
*Explanation:* Notice that the path doesn't cross any point more than once.
10+
如果路径在任何位置上与自身相交,也就是走到之前已经走过的位置,请返回 `true` ;否则,返回 `false`
1811

19-
----
12+
*示例 1:*
2013

21-
*Example 2:*
22-
<img alt="" src="https://assets.leetcode.com/uploads/2020/06/10/screen-shot-2020-06-10-at-123843-pm.png" style="width: 400px; height: 339px;" />
23-
[subs="verbatim,quotes"]
24-
----
25-
*Input:* path = "NESWW"
26-
*Output:* true
27-
*Explanation:* Notice that the path visits the origin twice.
28-
----
14+
image::images/1496-01.png[{image_attr}]
2915

30-
31-
*Constraints:*
16+
....
17+
输入:path = "NES"
18+
输出:false
19+
解释:该路径没有在任何位置相交。
20+
....
3221

22+
*示例 2:*
3323

34-
* `1 <= path.length <= 10^4^`
35-
* `path[i]` is either `'N'`, `'S'`, `'E'`, or `'W'`.
24+
image::images/1496-02.png[{image_attr}]
3625

26+
....
27+
输入:path = "NESWW"
28+
输出:true
29+
解释:该路径经过原点两次。
30+
....
3731

3832

33+
*提示:*
34+
35+
* `1 \<= path.length \<= 10^4^`
36+
* `path[i]``N``S``E``W`
37+
3938
4039
== 思路分析
4140

41+
想找个巧办法,结果失败!还是用记录轨迹的办法搞定了。
42+
43+
因为题目要求 `1 \<= path.length \<= 10^4^`,那么 `x` 和 `y` 的值,最大不会超过 `10^4^`,直接讲 `(x, y)` 转换成 `x * 10000 + y` 一个数字来作为坐标即可。
44+
4245

4346
[[src-1496]]
4447
[tabs]

docs/images/1496-01.png

33.2 KB
Loading

docs/images/1496-02.png

43.3 KB
Loading

docs/index.adoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3075,7 +3075,7 @@ include::1480-running-sum-of-1d-array.adoc[leveloffset=+1]
30753075

30763076
// include::1495-friendly-movies-streamed-last-month.adoc[leveloffset=+1]
30773077

3078-
// include::1496-path-crossing.adoc[leveloffset=+1]
3078+
include::1496-path-crossing.adoc[leveloffset=+1]
30793079

30803080
// include::1497-check-if-array-pairs-are-divisible-by-k.adoc[leveloffset=+1]
30813081

logbook/202503.adoc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -843,6 +843,11 @@ endif::[]
843843
|{doc_base_url}/1387-sort-integers-by-the-power-value.adoc[题解]
844844
|✅ 先计算每个数字的权重,再进行比较:使用集合存储下标,通过下标找到对应的权重。计算权重时,可以递归,也可以直接循环。使用递归时,可以使用备忘录把已经计算的值存储起来,防止重复计算。
845845

846+
|{counter:codes2503}
847+
|{leetcode_base_url}/path-crossing/[1496. 判断路径是否相交^]
848+
|{doc_base_url}/1496-path-crossing.adoc[题解]
849+
|✅ 想找个巧办法,结果失败!还是用记录轨迹的办法搞定了。
850+
846851
|===
847852

848853
截止目前,本轮练习一共完成 {codes2503} 道题。
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.diguage.algo.leetcode;
2+
3+
import java.util.HashSet;
4+
import java.util.Set;
5+
6+
public class _1496_PathCrossing {
7+
// tag::answer[]
8+
9+
/**
10+
* @author D瓜哥 · https://www.diguage.com
11+
* @since 2025-05-27 20:32:05
12+
*/
13+
public boolean isPathCrossing(String path) {
14+
int x = 0, y = 0;
15+
Set<Integer> visited = new HashSet<>();
16+
visited.add(0);
17+
for (int i = 0; i < path.length(); i++) {
18+
char c = path.charAt(i);
19+
if (c == 'N') {
20+
y++;
21+
} else if (c == 'S') {
22+
y--;
23+
} else if (c == 'E') {
24+
x++;
25+
} else if (c == 'W') {
26+
x--;
27+
}
28+
int point = x * 10000 + y;
29+
if (visited.contains(point)) {
30+
return true;
31+
} else {
32+
visited.add(point);
33+
}
34+
}
35+
return false;
36+
}
37+
// end::answer[]
38+
}

0 commit comments

Comments
 (0)