프로그래머스 (배열의 원소만큼 추가하기)
import java.util.ArrayList;
import java.util.List;
class Solution {
public int[] solution(int[] arr) {
List<Integer> tempList = new ArrayList<>();
// arr 배열의 길이 만큼 반복
for (int i = 0; i < arr.length; i++) {
// arr[i]의 값 만큼 반복
for (int j = 0; j < arr[i]; j++) {
tempList.add(arr[i]); // tempList에 값을 담아준다.
}
}
int[] answer = new int[tempList.size()];
for (int i = 0; i < answer.length; i++) {
answer[i] = tempList.get(i);
}
return answer;
}
}
프로그래머스 (길이에 따른 연산)
class Solution {
public int solution(int[] num_list) {
int answer = 0;
if(num_list.length >= 11){
for(int i = 0; i < num_list.length; i++){
answer += num_list[i];
}
}
else{
// 곱해야됨으로, answer를 1로 초기화
answer = 1;
for(int i = 0; i < num_list.length; i++){
answer *= num_list[i];
}
}
return answer;
}
}
프로그래머스 (마지막 두 원소)
import java.util.ArrayList;
import java.util.List;
public class problem379 {
class Solution {
public int[] solution(int[] num_list) {
int size = num_list.length;
List<Integer> tempList = new ArrayList<>();
// 마지막 원소에서 그전 원소를 뺀 값
int minus = num_list[size - 1] - num_list[size - 2];
//마지막 원소를 두 배한 값
int multi = num_list[size - 1] * 2;
for (int i = 0; i < num_list.length; i++) {
tempList.add(num_list[i]);
}
if (num_list[size - 1] > num_list[size - 2]) {
tempList.add(minus);
} else {
tempList.add(multi);
}
int[] answer = new int[tempList.size()];
for (int i = 0; i < answer.length; i++) {
answer[i] = tempList.get(i);
}
return answer;
}
}
}