본문으로 바로가기
728x90
반응형


 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class Solution {
    public int[] solution(int[] numbers, String direction) {
         int[] answer = new int[numbers.length];
         switch(direction) {
         case "left" :                                  //left로 이동시킬경우
             int temp=numbers[0];                       //매개변수로 들어온 배열의 첫번째 정수를 저장하고
             for(int i=0; i<numbers.length-1; i++) {
                 answer[i]=numbers[i+1];                //나머지 정수를 한칸씩 왼쪽으로 옮긴후
             }
             answer[numbers.length-1]=temp; break;      //저장했던 첫번째 정수를 마지막 자리에 넣음
         
         case "right" :                                 //right로 이동시킬경우
             int temp1=numbers[numbers.length-1];       //매개변수로 들어온 배열의 마지막 정수를 저장하고
             for(int i=numbers.length-1; i>0; i--) {
                 answer[i]=numbers[i-1];                //나머지 정수를 한칸씩 오른쪽으로 옮긴후
             }
             answer[0]=temp1; break;                    //저장했던 마지막 정수를 첫번째 자리에 넣음
         }
cs

 

반응형

 

728x90
반응형