YJ의 새벽

프로그래머스(0) - 배열 회전시키기 [JAVA] 본문

코딩테스트연습/프로그래머스

프로그래머스(0) - 배열 회전시키기 [JAVA]

YJDawn 2024. 3. 13. 15:49

 

 

 

 

class Solution {
    public int[] solution(int[] numbers, String direction) {
        int[] answer = new int[numbers.length];
        
        if(direction.equals("left")) {
        	for(int i=0; i<numbers.length-1; i++) {   // 왼쪽으로 한칸씩.
        		answer[i] = numbers[i+1];
        	}
        	answer[numbers.length-1] = numbers[0];  // 끝 인덱스에 0번재 값 넣어줌.
        }else {
        	for(int i=1; i<numbers.length; i++) {   // 오른쪽으로 한칸씩.
        		answer[i] = numbers[i-1];
        	}
        	answer[0] = numbers[numbers.length-1]; // 맨앞 인덱스에 끝값 넣어줌.
        }
        return answer;
    }
}
Comments