Skip to content

Commit b35341a

Browse files
committed
一刷1496
1 parent a1b0257 commit b35341a

File tree

8 files changed

+128
-63
lines changed

8 files changed

+128
-63
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} 道题。

pom.xml

Lines changed: 50 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -816,6 +816,24 @@
816816
</dependency>
817817
</dependencies>
818818
</plugin>
819+
<plugin>
820+
<groupId>org.codehaus.mojo</groupId>
821+
<artifactId>exec-maven-plugin</artifactId>
822+
<version>${exec-maven-plugin.version}</version>
823+
<executions>
824+
<execution>
825+
<id>install-fonts-and-generate-config-file</id>
826+
<phase>generate-sources</phase>
827+
<goals>
828+
<goal>exec</goal>
829+
</goals>
830+
<configuration>
831+
<executable>${project.basedir}/cfg/scripts/install-fonts.sh</executable>
832+
<!-- <commandlineArgs>Args</commandlineArgs> -->
833+
</configuration>
834+
</execution>
835+
</executions>
836+
</plugin>
819837
<plugin>
820838
<!-- 3.2.0 依赖 jruby 9.4.8.0/9.4.12.0 -->
821839
<groupId>org.asciidoctor</groupId>
@@ -825,8 +843,9 @@
825843
<sourceDirectory>docs</sourceDirectory>
826844
<sourceDocumentName>index.adoc</sourceDocumentName>
827845
<attributes>
828-
<project_basedir>${maven.multiModuleProjectDirectory}</project_basedir>
829-
<sourcedir>${project.build.sourceDirectory}/com/diguage/yongle</sourcedir>
846+
<sourcedir>${project.build.sourceDirectory}/com/diguage/algo/leetcode</sourcedir>
847+
<labdir>${project.build.testSourceDirectory}/com/diguage/labs</labdir>
848+
<basedir>${project.basedir}</basedir>
830849
<doctype>book</doctype>
831850
<stem>latexmath</stem>
832851
<source-highlighter>rouge</source-highlighter>
@@ -838,18 +857,23 @@
838857
<linkcss>true</linkcss>
839858
<stylesdir>assets/styles</stylesdir>
840859
<toc/>
841-
<toclevels>3</toclevels>
842-
<sectnums>true</sectnums>
860+
<toclevels>1</toclevels>
861+
<!-- <sectnums>false</sectnums>-->
843862
<sectnumlevels>3</sectnumlevels>
844863
<sectanchors>true</sectanchors>
845864
<graphvizdot>/usr/local/bin/dot</graphvizdot>
865+
<project_basedir>${project.basedir}</project_basedir>
846866
<source_attr>linenums,indent=0,subs="attributes,verbatim,quotes"</source_attr>
847-
<java_src_attr>source%nowrap,java,{source_attr}</java_src_attr>
867+
<java_src_attr>source%nowrap,java,linenums,indent=0,subs="attributes,verbatim"</java_src_attr>
848868
<html_src_attr>source%nowrap,html,{source_attr}</html_src_attr>
849869
<image_attr>align="center",width=95%</image_attr>
850870
<diagram_attr>format=svg,align="center",width=95%</diagram_attr>
851871
<plantumlconfig>${project.basedir}/cfg/plantuml.cfg</plantumlconfig>
872+
<toc-title>目录</toc-title>
852873
<iconfont-cdn>//cdn.jsdelivr.net/npm/font-awesome@4.7.0/css/font-awesome.min.css</iconfont-cdn>
874+
<doc_base_url>xref:.</doc_base_url>
875+
<leetcode>https://leetcode.cn</leetcode>
876+
<leetcode_base_url>https://leetcode.cn/problems</leetcode_base_url>
853877
</attributes>
854878
</configuration>
855879
<executions>
@@ -876,27 +900,27 @@
876900
</configuration>
877901
</execution>
878902

879-
<execution>
880-
<id>generate-html</id>
881-
<phase>package</phase>
882-
<goals>
883-
<goal>process-asciidoc</goal>
884-
</goals>
885-
<configuration>
886-
<backend>html5</backend>
887-
<doctype>book</doctype>
888-
<outputDirectory>${project.build.directory}/docs/html</outputDirectory>
889-
<attributes>
890-
<toc>left</toc>
891-
</attributes>
892-
<gemPath>${gem.path}</gemPath>
893-
<requires>
894-
<require>asciidoctor-tabs</require>
895-
<require>asciidoctor-diagram</require>
896-
<require>asciidoctor-comment-links</require>
897-
</requires>
898-
</configuration>
899-
</execution>
903+
<!-- <execution>-->
904+
<!-- <id>generate-html</id>-->
905+
<!-- <phase>package</phase>-->
906+
<!-- <goals>-->
907+
<!-- <goal>process-asciidoc</goal>-->
908+
<!-- </goals>-->
909+
<!-- <configuration>-->
910+
<!-- <backend>html5</backend>-->
911+
<!-- <doctype>book</doctype>-->
912+
<!-- <outputDirectory>${project.build.directory}/docs/html</outputDirectory>-->
913+
<!-- <attributes>-->
914+
<!-- <toc>left</toc>-->
915+
<!-- </attributes>-->
916+
<!-- <gemPath>${gem.path}</gemPath>-->
917+
<!-- <requires>-->
918+
<!-- <require>asciidoctor-tabs</require>-->
919+
<!-- <require>asciidoctor-diagram</require>-->
920+
<!-- <require>asciidoctor-comment-links</require>-->
921+
<!-- </requires>-->
922+
<!-- </configuration>-->
923+
<!-- </execution>-->
900924

901925
<!-- <execution>-->
902926
<!-- <id>generate-epub</id>-->
@@ -973,11 +997,6 @@
973997
<artifactId>asciidoctorj-diagram</artifactId>
974998
<version>${asciidoctorj-diagram.version}</version>
975999
</dependency>
976-
<dependency>
977-
<groupId>org.asciidoctor</groupId>
978-
<artifactId>asciidoctorj-diagram-plantuml</artifactId>
979-
<version>1.2025.2</version>
980-
</dependency>
9811000
<dependency>
9821001
<groupId>org.jruby</groupId>
9831002
<artifactId>jruby</artifactId>
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)