解题思路:
(1)建立map,判断每个横坐标是否对应重复的纵坐标
class Solution {
public:
bool isPathCrossing(string path) {
int x=0,y=0;
unordered_map<int,vector<int>> mp;
vector<int>::iterator it;
mp[0].push_back(0);
for(int i=0;i<path.size();i++) {
switch(path[i]) {
case 'N':
y++;
it = find(mp[x].begin(),mp[x].end(),y);
if(it!=mp[x].end()) return true;
else mp[x].push_back(y);
break;
case 'W':
x--;
it = find(mp[x].begin(),mp[x].end(),y);
if(it!=mp[x].end()) return true;
else mp[x].push_back(y);
break;
case 'E':
x++;
it = find(mp[x].begin(),mp[x].end(),y);
if(it!=mp[x].end()) return true;
else mp[x].push_back(y);
break;
case 'S':
y--;
it = find(mp[x].begin(),mp[x].end(),y);
if(it!=mp[x].end()) return true;
else mp[x].push_back(y);
break;
}
}
return false;
}
};