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;
}
}
'๐ป Coding Problems Solving > Hash | HashMap' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[LeetCode] First Unique Character in a String (0) | 2023.07.20 |
---|---|
[LeetCode] Group Anagrams (0) | 2023.07.18 |
[ํ๋ก๊ทธ๋๋จธ์ค] LV.2 ์ฃผ์ฐจ ์๊ธ ๊ณ์ฐ (java) (0) | 2023.03.04 |
[ํ๋ก๊ทธ๋๋จธ์ค] LV.2 [3์ฐจ] ์์ถ (0) | 2022.04.25 |
[ํ๋ก๊ทธ๋๋จธ์ค] LV.2 ์ ํ๋ฒํธ ๋ชฉ๋ก (0) | 2022.04.12 |
์ต๊ทผ๋๊ธ