-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKeyPad.java
66 lines (56 loc) · 1.85 KB
/
KeyPad.java
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
59
60
61
62
63
64
65
66
class Pos{
int x;
int y;
public Pos(int y, int x){
this.y = y;
this.x = x;
}
}
class Solution {
public static String solution(int[] numbers, String hand) {
String answer = "";
Pos left = new Pos(3, 0); // *에서 시작
Pos right = new Pos(3, 2); // #에서 시작
Pos key[] = new Pos[10];
int cnt = 1;
key[0] = new Pos(3,1); // 0 위치
for(int i=0; i<3; i++){
for(int j=0; j<3; j++)
key[cnt++] = new Pos(i,j);
}
for(int i=0; i<numbers.length; i++){
Pos next = key[numbers[i]];
if((next.y == 0 || next.y == 1 || next.y == 2) && next.x == 0) { // 1, 4, 7 인 경우
left = next;
answer += "L";
}
else if((next.y == 0 || next.y == 1 || next.y == 2) && next.x == 2) { // 3, 6, 9인 경우
right = next;
answer += "R";
}
else{ // 2, 5, 8, 0 인 경우
int leftDis = Math.abs(left.x - next.x) + Math.abs(left.y - next.y);
int rightDis = Math.abs(right.x - next.x) + Math.abs(right.y - next.y);
if(hand.equals("left")) {
if(leftDis<= rightDis){
answer+= "L";
left = next;
}else{
answer +="R";
right = next;
}
}
else if(hand.equals("right")) {
if(leftDis >= rightDis){
answer+= "R";
right=next;
}else{
answer += "L";
left = next;
}
}
}
}
return answer;
}
}