Path Crossing
给一个string, 里面是东南西北, 给一个点从(0,0)开始, 求是否重复了坐标.
模拟即可
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
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; } } |