문제 링크 : Path Crossing
/**
* @param {string} path
* @return {boolean}
*/
var isPathCrossing = function(path) {
const way = [[0,0]]
for(let i=0; i<path.length; i++) {
let tempWay = [...way[way.length-1]]
if(path[i] === 'N') {
tempWay[1] += 1
} else if(path[i] === 'S') {
tempWay[1] -= 1
} else if(path[i] === 'E') {
tempWay[0] += 1
} else if(path[i] === 'W') {
tempWay[0] -= 1
}
if( way.filter(e => e[0] === tempWay[0] && e[1] === tempWay[1]).length > 0 ) {
return true
} else {
way.push(tempWay)
}
}
return false
};
/**
* @param {string} path
* @return {boolean}
*/
var isPathCrossing = function(path) {
let pathMap = new Map()
let x = 0
let y = 0
let coord = `${x},${y}`
pathMap.set(coord,true)
for(let p of path){
switch(p){
case 'N':
y++;
break
case 'S':
y--;
break
case 'E':
x++;
break;
case 'W':
x--;
break;
}
coord = `${x},${y}`
if(pathMap.has(coord)){
return true
}
pathMap.set(coord,true)
}
return false
};