1. ๋ฌธ์ œ :https://school.programmers.co.kr/learn/courses/30/lessons/49994

 

2. ํ’€์ด

์ขŒํ‘œ๊ฐ’์„ ๋ฐฐ์—ด๋กœ ํ™•์ธํ•  ์ˆ˜ ์—†๊ธฐ ๋•Œ๋ฌธ์— ์ด๋™ ๊ฒฝ๋กœ๋ฅผ hashmap์— ๋„ฃ์–ด์„œ ์ค‘๋ณต ์ฒดํฌ

 

3. ์ฝ”๋“œ

import java.util.*;

class Solution {
    public int solution(String dirs) {
        int answer = 0;
        HashSet<String> s = new HashSet<String>();  //์ค‘๋ณตํ™•์ธ ์œ„ํ•ด ์‚ฌ์šฉํ•˜๋Š” set
        
        int len = dirs.length();
        
        int nowX = 0;
        int nowY = 0;
        
        for(int i=0; i<len; i++){
            int nextX = nowX;
            int nextY = nowY;
            String road = "";       //๊ฒฝ๋กœ ์ €์žฅํ•  ๋ฌธ์ž์—ด
            // U : "ํ˜„์žฌ ์ขŒํ‘œ"+"์ด๋™ ํ›„ ์ขŒํ‘œ"
            if(dirs.charAt(i) == 'U'){
                nextY++;
                road += nowX;
                road += nowY;
                road += nextX;
                road += nextY;
            }
            // D :  "์ด๋™ ํ›„ ์ขŒํ‘œ" + "ํ˜„์žฌ ์ขŒํ‘œ"
            else if(dirs.charAt(i) == 'D'){
                nextY--;
                road += nextX;
                road += nextY;
                road += nowX;
                road += nowY;
            }
            // R : "ํ˜„์žฌ ์ขŒํ‘œ"+"์ด๋™ ํ›„ ์ขŒํ‘œ"
            else if(dirs.charAt(i) == 'R'){
                nextX++;
                road += nowX;
                road += nowY;
                road += nextX;
                road += nextY;
            }
            // L :  "์ด๋™ ํ›„ ์ขŒํ‘œ" + "ํ˜„์žฌ ์ขŒํ‘œ"
            else if(dirs.charAt(i) == 'L'){
                nextX--;
                road += nextX;
                road += nextY;
                road += nowX;
                road += nowY;
            }
            
            System.out.println(road);
            //๋ฒ”์œ„ ๋ฒ—์–ด๋‚˜๋ฉด ๋ฌด์‹œ
            if(nextX < -5 || nextY < -5 || nextX > 5 || nextY > 5)
                continue;
            
            s.add(road);
            nowX = nextX;
            nowY = nextY;
        }
        answer = s.size();
        return answer;
    }
}
  • ๋„ค์ด๋ฒ„ ๋ธ”๋Ÿฌ๊ทธ ๊ณต์œ ํ•˜๊ธฐ
  • ๋„ค์ด๋ฒ„ ๋ฐด๋“œ์— ๊ณต์œ ํ•˜๊ธฐ
  • ํŽ˜์ด์Šค๋ถ ๊ณต์œ ํ•˜๊ธฐ
  • ์นด์นด์˜ค์Šคํ† ๋ฆฌ ๊ณต์œ ํ•˜๊ธฐ