# Judging whether paths intersect or not by leetcode

2020-11-07 21:43:17 go4it

## subject

`````` Give you a string  path, among  path[i]  The value of can be  'N'、'S'、'E'  perhaps  'W', To the north 、 Southward 、 To the east 、 Move one unit West .

The robot starts from the origin on the two-dimensional plane  (0, 0)  Starting from , Press  path  The path indicated to walk .

If paths intersect at any point , That is to go to the position that has been passed before , Please return  True ; otherwise , return  False .

source ： Power button （LeetCode）
``````

``````class Solution {
public boolean isPathCrossing(String path) {
int x = 0;
int y = 0;
Set<String> pathSet = new HashSet<String>();
for (char c : path.toCharArray()) {
if (c == 'N') {
y++;
} else if (c == 'S') {
y--;
} else if (c == 'W') {
x--;
} else if (c == 'E') {
x++;
}
String p = String.valueOf(x) + String.valueOf(y);
if (pathSet.contains(p)) {
return true;
}
}
return false;
}
}
``````

## Summary

Here to maintain the past point , Then traverse path The characters of , Yes x,y The coordinates move accordingly , After each move, judge whether the point has passed , Walk past and return true, If not, record the change points in the past points , After traversing, it will return if it does not meet the conditions false.