Skip to content

Commit

Permalink
Create KeyPad.java
Browse files Browse the repository at this point in the history
  • Loading branch information
sey2 authored Aug 11, 2022
1 parent cb59cfb commit 3eb8219
Showing 1 changed file with 66 additions and 0 deletions.
66 changes: 66 additions & 0 deletions programmers/KeyPad.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,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;
}
}

0 comments on commit 3eb8219

Please sign in to comment.