Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 9ad9a96

Browse files
add 1496
1 parent 86a8886 commit 9ad9a96

File tree

3 files changed

+89
-0
lines changed

3 files changed

+89
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ _If you like this project, please leave me a star._ ★
88

99
| # | Title | Solutions | Video | Difficulty | Tag
1010
|-----|----------------|---------------|--------|-------------|-------------
11+
|1496|[Path Crossing](https://leetcode.com/problems/path-crossing/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1496.java) | |Easy|String|
1112
|1493|[Longest Subarray of 1's After Deleting One Element](https://leetcode.com/problems/longest-subarray-of-1s-after-deleting-one-element/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1493.java) | |Medium|Array|
1213
|1492|[The kth Factor of n](https://leetcode.com/problems/the-kth-factor-of-n/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1492.java) | |Medium|Math|
1314
|1491|[Average Salary Excluding the Minimum and Maximum Salary](https://leetcode.com/problems/average-salary-excluding-the-minimum-and-maximum-salary/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1491.java) | |Easy|Array, Sort|
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package com.fishercoder.solutions;
2+
3+
import java.util.Objects;
4+
import java.util.Stack;
5+
6+
public class _1496 {
7+
public static class Solution1 {
8+
public boolean isPathCrossing(String path) {
9+
Stack<Coord> visited = new Stack<>();
10+
visited.add(new Coord(0, 0));
11+
for (char c : path.toCharArray()) {
12+
Coord last = visited.peek();
13+
if (c == 'N') {
14+
Coord nextStep = new Coord(last.x, last.y + 1);
15+
if (visited.contains(nextStep)) {
16+
return true;
17+
}
18+
visited.add(nextStep);
19+
} else if (c == 'S') {
20+
Coord nextStep = new Coord(last.x, last.y - 1);
21+
if (visited.contains(nextStep)) {
22+
return true;
23+
}
24+
visited.add(nextStep);
25+
} else if (c == 'E') {
26+
Coord nextStep = new Coord(last.x - 1, last.y);
27+
if (visited.contains(nextStep)) {
28+
return true;
29+
}
30+
visited.add(nextStep);
31+
} else if (c == 'W') {
32+
Coord nextStep = new Coord(last.x + 1, last.y);
33+
if (visited.contains(nextStep)) {
34+
return true;
35+
}
36+
visited.add(nextStep);
37+
}
38+
}
39+
return false;
40+
}
41+
42+
static class Coord {
43+
int x;
44+
int y;
45+
46+
public Coord(int x, int y) {
47+
this.x = x;
48+
this.y = y;
49+
}
50+
51+
@Override
52+
public boolean equals(Object o) {
53+
if (this == o) return true;
54+
if (o == null || getClass() != o.getClass()) return false;
55+
Coord coord = (Coord) o;
56+
return x == coord.x &&
57+
y == coord.y;
58+
}
59+
60+
@Override
61+
public int hashCode() {
62+
return Objects.hash(x, y);
63+
}
64+
}
65+
}
66+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._1496;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static junit.framework.TestCase.assertEquals;
8+
9+
public class _1496Test {
10+
private static _1496.Solution1 solution1;
11+
12+
@BeforeClass
13+
public static void setup() {
14+
solution1 = new _1496.Solution1();
15+
}
16+
17+
@Test
18+
public void test1() {
19+
assertEquals(false, solution1.isPathCrossing("NES"));
20+
}
21+
22+
}

0 commit comments

Comments
 (0)