내 답안
class Solution {
public int[] solution(int[] arr, int n) {
int[] answer = new int[arr.length];
for (int i = 0; i < arr.length; i++) {
if (arr.length % 2 == 1 && i % 2 == 0) {
answer[i] = arr[i] + n;
} else if (arr.length % 2 == 0 && i % 2 == 1) {
answer[i] = arr[i] + n;
} else {
answer[i] = arr[i];
}
}
return answer;
}
}
Java
복사
다른사람 풀이
class Solution {
public int[] solution(int[] arr, int n) {
for(int idx=arr.length%2==0?1:0; idx<arr.length; idx+=2) {
arr[idx]+=n;
}
return arr;
}
}
Java
복사
class Solution {
public int[] solution(int[] arr, int n) {
for (int i = arr.length - 1; i >= 0; i -= 2) {
arr[i] += n;
}
return arr;
}
}
Java
복사