728x90
SMALL

using System;

public class Solution {
    public int[] solution(int[] numbers, string direction) {
        int[] answer = new int[numbers.Length];
        
        if(direction == "right")
        {
            for(int i=0;i<numbers.Length-1;i++)
            {
                answer[0] = numbers[numbers.Length-1]; // 가장 오른쪽 배열을 0으로 이동
                answer[i+1] = numbers[i]; // 한 칸씩 이동
            }
        }
        else if(direction == "left")
        {
            for(int j=1;j<numbers.Length;j++)
            {
                answer[numbers.Length-1] = numbers[0]; // 맨 오른쪽 배열은 0번째로
                answer[j-1] = numbers[j]; // -1칸씩 이동
            }
        }
            
        return answer;
    }
}

 

파이썬

 

def solution(numbers, direction):
    return [numbers[-1]] + numbers[:-1] if direction == 'right' else numbers[1:] + [numbers[0]]

//////

def solution(numbers, direction):
    if direction == "right":
        answer = [numbers[-1]] + numbers[:len(numbers)-1]
    else:
        answer = numbers[1:] + [numbers[0]]
    return answer

 

자바

 

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

class Solution {
    public int[] solution(int[] numbers, String direction) {
        List<Integer> list = Arrays.stream(numbers).boxed().collect(Collectors.toList());

        if (direction.equals("right")) 
        {
            list.add(0, list.get(list.size() - 1));
            list.remove(list.size() - 1);
        } 
        else 
        {
            list.add(list.size(), list.get(0));
            list.remove(0);
        }
        return list.stream().mapToInt(Integer::intValue).toArray();
    }
}

 

 

import java.util.*;

class Solution {
    public ArrayList solution(int[] numbers, String direction) {
        ArrayList<Integer> answer = new ArrayList<Integer>();

        for(int i=0; i<numbers.length; i++)
        {
            answer.add(numbers[i]);
        }

        int targetValue = 0;

        if(direction.equals("right"))
        {
            answer.add(0, numbers[numbers.length-1]);
            answer.remove(answer.size()-1);

        }
        else
        {
            answer.add(numbers[0]);
            answer.remove(0);
        }

        return answer;
    }
}

 

 

class Solution {
    public int[] solution(int[] numbers, String direction) {
        int[] answer = new int[numbers.length];

        if(direction.equals("right"))
        {
            for(int i = 0; i<numbers.length; i++)
            {
                if(i==numbers.length-1)
                {
                    answer[0] = numbers[i];
                }
                else answer[i+1] = numbers[i];
            }
        }
        else
        {
            for(int i = 1; i<numbers.length; i++)
            {
                answer[i-1] = numbers[i];
            }
            
            answer[numbers.length-1] = numbers[0];
        }

        return answer;
    }
}
728x90

설정

트랙백

댓글