【题目】5448. 判断路径是否相交
给你一个字符串 path,其中 path[i] 的值可以是 ‘N’、‘S’、‘E’ 或者 ‘W’,分别表示向北、向南、向东、向西移动一个单位。
机器人从二维平面上的原点 (0, 0) 处开始出发,按 path 所指示的路径行走。
如果路径在任何位置上出现相交的情况,也就是走到之前已经走过的位置,请返回 True ;否则,返回 False 。
示例 1:
输入:path = "NES"
输出:false
解释:该路径没有在任何位置相交。
示例 2:
输入:path = "NESWW"
输出:true
解释:该路径经过原点两次。
提示:
1 <= path.length <= 10^4
path 仅由 {‘N’, ‘S’, ‘E’, 'W} 中的字符组成
【解题思路1】Set,将座标转化为string存
set是容易想到的,但是不能存储键值对,而map又会覆盖value,所以存字符串模拟座标。
class Solution {
public boolean isPathCrossing(String path) {
int x=0;
int y=0;
Set<String> set=new HashSet<>();
set.add("0,0");
for(int k=0;k<path.length();k++){
char c=path.charAt(k);
if(c=='N')y++;
if(c=='S')y--;
if(c=='E')x++;
if(c=='W')x--;
String temp=x+","+y;
if(set.contains(temp)){
return true;
}else{
set.add(temp);
}
}
return false;
}
}
【解题思路2】Set<Pair<Integer, Integer>>
class Solution {
public boolean isPathCrossing(String path) {
Set<Pair<Integer, Integer>> set = new HashSet<>();
int x = 0, y = 0;
set.add(new Pair(x, y));
for (char c: path.toCharArray()) {
switch (c) {
case 'N':
y++;
break;
case 'S':
y--;
break;
case 'E':
x++;
break;
case 'W':
x--;
break;
}
Pair pair = new Pair(x, y);
if (set.contains(pair))
return true;
set.add(pair);
}
return false;
}
}