๋ฐ์ํ
๐ ๋ฌธ์ URL ๐
2,5,8,0์ผ ๊ฒฝ์ฐ ์ค๋ฅธ์, ์ผ์์ด ์์นํ ํคํจ๋์์์ ๊ฑฐ๋ฆฌ๋ฅผ ๊ตฌํด์ผ ํ๊ธฐ ๋๋ฌธ์ ์ขํ๋ฅผ ์ด์ฉํ์ฌ ํ์๋ค.
๊ทธ๋ฆฌ๊ณ Stream API์ ์๋ anyMatch๋ฅผ ์ด์ฉํ์ฌ ์ต์ํ ํ ๊ฐ์ ์์๊ฐ ์ฃผ์ด์ง ์กฐ๊ฑด์ ๋ง์กฑํ๋์ง๋ฅผ ์ฒดํฌํ์๋ค.
import java.util.Arrays;
class Solution {
public String solution(int[] numbers, String hand) {
String answer = "";
int[] leftHand = {1,4,7};
int[] rightHand = {3,6,9};
int[] leftHandLocation = {3,0};
int[] rightHandLocation = {3,2};
StringBuffer sb = new StringBuffer();
for(int num : numbers){
//1,4,7์ธ ๊ฒฝ์ฐ
if(Arrays.stream(leftHand).anyMatch( a -> a==num)){
sb.append("L");
leftHandLocation = getIndex(num);
//3,6,9์ธ ๊ฒฝ์ฐ
}else if(Arrays.stream(rightHand).anyMatch( a -> a==num)){
sb.append("R");
rightHandLocation = getIndex(num);
//2,5,8,0 ์ธ ๊ฒฝ์ฐ
}else{
int[] index = getIndex(num);
int rightResult = Math.abs(index[0] - rightHandLocation[0])+ Math.abs(index[1] - rightHandLocation[1]);
int leftResult = Math.abs(index[0] - leftHandLocation[0])+ Math.abs(index[1] - leftHandLocation[1]);
if(rightResult>leftResult){
sb.append("L");
leftHandLocation = getIndex(num);
}else if(rightResult<leftResult){
sb.append("R");
rightHandLocation = getIndex(num);
}else{
hand = hand.toUpperCase().substring(0,1);
if(hand.equals("R")){
rightHandLocation = getIndex(num);
}else{
leftHandLocation = getIndex(num);
}
sb.append(hand);
}
}
}
answer = sb.toString();
return answer;
}
//์ขํ ๊ตฌํ๊ธฐ. [0]์ x์ขํ, [1]์ y์ขํ
public int[] getIndex(int num){
int[][] pad = {{1, 2, 3}, {4,5,6,}, {7,8,9}, {10,0,11}};
int[] result = new int[2];
for(int i =0 ; i<pad.length; i++){
for(int j=0; j<pad[i].length; j++){
if(pad[i][j] == num){
result[0] = i;
result[1] = j;
}
}
}
return result;
}
}
728x90
๋ฐ์ํ
'๊ฐ๋ฐ > ์๊ณ ๋ฆฌ์ฆ' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[ํ๋ก๊ทธ๋๋จธ์ค/JAVA] ์ฒด์ก๋ณต (0) | 2022.07.25 |
---|---|
[ํ๋ก๊ทธ๋๋จธ์ค/JAVA] ์ซ์ ๋ฌธ์์ด๊ณผ ์๋จ์ด (0) | 2022.07.24 |
[ํ๋ก๊ทธ๋๋จธ์ค/JAVA] ์ ๊ท ์์ด๋ ์ถ์ฒ (0) | 2022.07.24 |
[ํ๋ก๊ทธ๋๋จธ์ค/JAVA] ๋ก๋์ ์ต๊ณ ์์์ ์ต์ ์์ (0) | 2022.07.24 |
[ํ๋ก๊ทธ๋๋จธ์ค/JAVA] ์ ๊ณ ๊ฒฐ๊ณผ ๋ฐ๊ธฐ (1) | 2022.07.24 |
๋๊ธ