Path Crossing
给一个string, 里面是东南西北, 给一个点从(0,0)开始, 求是否重复了坐标.
模拟即可
class Solution {
public boolean isPathCrossing(String path) {
int x = 0;
int y = 0;
Set<String> set = new HashSet<>();
set.add(x+"|"+y);
for(char c : path.toCharArray()) {
if(c == 'N'){
y++;
} else if(c == 'S') {
y--;
} else if(c == 'W') {
x--;
} else{
x++;
}
if(set.contains(x+"|"+y))
return true;
set.add(x+"|"+y);
}
return false;
}
}