[JAVA] 프로그래머스 : 배열 회전시키기

조예빈·2024년 8월 24일
0

Coding Test

목록 보기
119/138

https://school.programmers.co.kr/learn/courses/30/lessons/120844

문자열의 내용을 비교할 때는 equals 메소드를 사용해 주어야 한다.

class Solution {
    public int[] solution(int[] numbers, String direction) {
        //numbers의 원소를 direction방향으로 한 칸씩 회전
        //direction == right : index + 1
        //direction == left : index -1
        int[] answer = new int[numbers.length];
        
        if(direction.equals("right")){
            for(int i=0; i<numbers.length; i++){
                answer[(i+1) % numbers.length] = numbers[i];
            }
        }else{
            for(int i=0; i<numbers.length; i++){
                answer[(i+numbers.length-1) % numbers.length] = numbers[i];
            }
            
        }
        
        return answer;
    }
}

실수로 창을 꺼버리는 바람에 몇 점 짜리 문제인지는 보지 못하였다ㅠㅠ

profile
컴퓨터가 이해하는 코드는 바보도 작성할 수 있다. 사람이 이해하도록 작성하는 프로그래머가 진정한 실력자다. -마틴 파울러

0개의 댓글