방명록
- [프로그래머스 / 자바] 배열 회전시키기2023년 10월 23일 12시 49분 12초에 업로드 된 글입니다.작성자: 민발자728x90
문제
정수가 담긴 배열 numbers와 문자열 direction가 매개변수로 주어집니다. 배열 numbers의 원소를 direction방향으로 한 칸씩 회전시킨 배열을 return하도록 solution 함수를 완성해주세요.
풀이
class Solution { public int[] solution(int[] numbers, String direction) { int[] answer = new int[numbers.length]; if(direction.equals("right")) { answer[0] = numbers[numbers.length-1]; //3 for(int i = 0; i < numbers.length-1; i++){ answer[i+1] = numbers[i]; } } else { int tmp = numbers[0]; for(int i = 0; i < numbers.length-1; i++) { answer[i] = numbers[i+1]; } answer[numbers.length-1] = tmp; } return answer; } }
갑자기 머리가 굳었나🤔 헷갈려서 시간 오래 걸렸다
728x90'기록 > 프로그래머스' 카테고리의 다른 글
[프로그래머스 / 자바] 팩토리얼 (0) 2023.10.23 [프로그래머스 / 자바] 합성수 구하기 (0) 2023.10.23 [프로그래머스 / 자바] 2차원으로 만들기 (0) 2023.10.23 [프로그래머스] 모스부호(1) (1) 2023.10.20 [프로그래머스] 순서쌍 구하기 (1) 2023.10.20 다음글이 없습니다.이전글이 없습니다.댓글