😎풀이

  1. Set 정의
  2. path 순회
    2-1. 동서남북 처리
    2-2. 현재 위치를 경유했었다면 true 반환
    2-3. 현재 위치 기록
  3. 어느 지점도 경유한 지점이 아니므로 false 반환
function isPathCrossing(path: string): boolean {
    const history = new Set()
    let x = 0
    let y = 0
    history.add(`${x},${y}`)
    for(const direct of path) {
        switch(direct) {
            case 'E':
                x++
                break
            case 'W':
                x--
                break
            case 'S':
                y++
                break
            case 'N':
                y--
                break
            default:
                break
        }
        const curPos = `${x},${y}`
        if(history.has(curPos)) return true
        history.add(curPos)
    }
    return false
};
profile
내 지식을 공유할 수 있는 대담함

0개의 댓글