1496. 判断路径是否相交
java代码:
class Solution {
public boolean isPathCrossing(String path) {
int x = 0;
int y = 0;
HashSet<String> hashSet = new HashSet<>();
hashSet.add("0-0");
for (int i = 0; i < path.length(); i++) {
switch (path.charAt(i)) {
case 'N': y++; break;
case 'S': y--; break;
case 'E': x++; break;
case 'W': x--; break;
}
String pos = x + "-" + y;
if (hashSet.contains(pos)) {
return true;
} else {
hashSet.add(pos);
}
}
return false;
}
}