YJ의 새벽
프로그래머스(0) - 배열 회전시키기 [JAVA] 본문
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;
}
}
'코딩테스트연습 > 프로그래머스' 카테고리의 다른 글
프로그래머스(0) - 문자열 정렬하기(2) [JAVA] (0) | 2024.03.13 |
---|---|
프로그래머스(0) - 숫자 찾기 [JAVA] (0) | 2024.03.13 |
프로그래머스(0) - 약수 구하기 [JAVA] (0) | 2024.03.13 |
프로그래머스(0) - 인덱스 바꾸기 [JAVA] (0) | 2024.03.13 |
프로그래머스(0) - 문자열 정렬하기(1) [JAVA] (0) | 2024.03.13 |
Comments