[프로그래머스 C#] Lv.0 배열 회전시키기

김병찬·2022년 11월 7일
1

프로그래머스 Lv.0

목록 보기
58/100

🎯문제설명

정수가 담긴 배열 numbers와 문자열 direction가 매개변수로 주어집니다. 배열 numbers의 원소를 direction방향으로 한 칸씩 회전시킨 배열을 return하도록 solution 함수를 완성해주세요.


❌제한사항

  • 3 ≤ numbers의 길이 ≤ 20
  • direction은 "left" 와 "right" 둘 중 하나입니다.

💬입출력 예

num1directionresult
[1, 2, 3]"right"[3, 1, 2]
[4, 455, 6, 4, -1, 45, 6]"left"[455, 6, 4, -1, 45, 6, 4]

💬입출력 예 설명

입출력 예 #1

  • numbers 가 [1, 2, 3]이고 direction이 "right" 이므로 오른쪽으로 한 칸씩 회전시킨 [3, 1, 2]를 return합니다.

입출력 예 #2

  • numbers 가 [4, 455, 6, 4, -1, 45, 6]이고 direction이 "left" 이므로 왼쪽으로 한 칸씩 회전시킨 [455, 6, 4, -1, 45, 6, 4]를 return합니다.

🔥나의 풀이

public class Solution {
    public int[] solution(int[] numbers, string direction) {
        int[] answer = new int[numbers.Length];
        int num = 0;
        
        if(direction == "right")
        {
            num = 1;
        }
        else
        {
            num = numbers.Length - 1;
        }
        
        
        for(int i = 0; i < numbers.Length; i++)
        {
            if(num == numbers.Length)
            {
                num = 0;
            }
            answer[num] = numbers[i];
            num++;
        }
        return answer;
    }
}

출처: 프로그래머스 코딩 테스트 연습, https://school.programmers.co.kr/learn/challenges

profile
[중요한건 꺾이지 않는 마음] Unity Developer

0개의 댓글