An array is a type of data structure that stores elements of the same type in a contiguous block of memory. In an array, , of size , each memory location has some unique index, (where ), that can be referenced as (you may also see it written as ).
Given an array, , of integers, print each element in reverse order as a single line of space-separated integers.
Note: If you've already solved our C++ domain's Arrays Introduction challenge, you may want to skip this.
Input Format
The first line contains an integer, (the number of integers in ).
The second line contains space-separated integers describing .
Constraints
Output Format
Print all integers in in reverse order as a single line of space-separated integers.
Sample Input 0
4
1 4 3 2
Sample Output 0
2 3 4 1
풀이
간단한 배열 뒤집기 문제이다.
for문을 이용할 경우 index를 활용하면 쉽게 풀 수 있다.
1. array의 length만큼의 새로운 배열 results를 만든다.
2. for문을 돌려서
result[0] = a[3]
result[1] = a[2]
result[2] = a[1]
result[3] = a[0]
이 되게끔 index를 조정해준다.
static int[] reverseArray(int[] a) {
/*
* Write your code here.
*/
int[] results = new int[a.length];
for(int i = 0; i < a.length; i++){
results[i] = a[a.length - 1 - i];
}
return results;
}
'DataStructure > Array' 카테고리의 다른 글
Add One To Number (0) | 2018.10.15 |
---|---|
Min Steps in Infinite Grid (0) | 2018.10.15 |
Left Rotation (0) | 2018.04.19 |
Dynamic Array (0) | 2018.04.19 |
2D Array - DS (0) | 2018.04.13 |