Repository navigation
Expand file tree
/
Copy path키패드 누르기
More file actions
58 lines (55 loc) · 2.56 KB
/
Copy path키패드 누르기
File metadata and controls
58 lines (55 loc) · 2.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import java.util.*;
class Solution {
public String solution(int[] numbers, String hand) {
HashMap<Integer,Integer> hash = new HashMap<Integer,Integer>();
hash.put(1,1);hash.put(2,1);hash.put(3,1);
hash.put(4,2);hash.put(5,2);hash.put(6,2);
hash.put(7,3);hash.put(8,3);hash.put(9,3);
hash.put(10,4);hash.put(0,4);hash.put(12,4);
String answer = "";
int left=10;
int right=12;
for(int i : numbers){
if(i%3==1){ // 왼쪽 일경우 왼쪽 손가락
left=i;
answer= answer.concat("L");
}
else if(i%3==0 && i!=0){ // 오른쪽일경우 오른쪽 손가락
right=i;
answer= answer.concat("R");
}
else{ // 가운데일경우
int L_len=Math.abs(hash.get(left)-hash.get(i)); // 왼쪽으로부터의 거리
int R_len=Math.abs(hash.get(right)-hash.get(i)); // 오른쪽으로부터의 거리
if(right!=0&& right%3!=2){
R_len++;
}
if(left!=0 && left%3!=2){
L_len++;
}
if(R_len>L_len){ // 값 비교
left=i;
answer=answer.concat("L");
}
else if(L_len>R_len){
right=i;
answer=answer.concat("R");
}
else{ // 값이 같다면 어느쪽손잡이인가
if(hand.equals("right")){
right=i;
answer=answer.concat("R");
}
else{
left=i;
answer=answer.concat("L");
}
}
}
}
return answer;
}
}
*************************************************************************************************************
키패드의 위치는 정해져 있다. 그리고 이 문제를 풀 때 주의점은 만약 2,3,5,0일경우 양쪽 손가락 거리를 구해야하느네
점과 점 사이의 거리 방정식으로 풀면안된다. 왜냐하면 문제를 보면 엄지손가락은 상하좌우로만 이동가능하다 적혀있다.