728x90
반응형
목차
개요
https://school.programmers.co.kr/learn/courses/30/lessons/120899
1. Solution
public int[] solution(int[] array) {
int current_max = 0;
int current_index = 0;
int[] answer = {0, 0};
for (int i = 0; i < array.length; i++) {
if (current_max < array[i]) {
current_max = array[i];
current_index = i;
}
}
answer[0] = current_max;
answer[1] = current_index;
return array;
}
2. Other Solution #1
public void otherSolution(int[] array) {
int[] answer = new int[2];
for (int i = 0; i < array.length; i++) {
if (array[i] > answer[0]) {
answer[0] = array[i];
answer[1] = i;
}
}
for (int e : answer) {
System.out.println(e);
}
}
3. Other Solution #2
public int[] solution(int[] array) {
int[] answer = new int[2];
int maxNumber = 0;
List<Integer> arrayInt = new ArrayList<>();
for (int a : array) {
arrayInt.add(a);
maxNumber = Math.max(maxNumber, a);
}
answer[0] = maxNumber;
answer[1] = arrayInt.indexOf(maxNumber);
return answer;
}
728x90
반응형
'Algorithm > Programmers' 카테고리의 다른 글
[Programmers][Java] 모음 제거 (0) | 2024.08.11 |
---|---|
[Programers] 코딩테스트 입문 #6 (with Python) (1) | 2024.05.01 |
[Programmers] 코딩테스트 입문 #5 (with Python) (0) | 2024.04.30 |
[Programmers] 코딩테스트 입문 #4 (with Python) (0) | 2024.04.29 |
[Programmers] 코딩테스트 입문 #3 (with Python) (0) | 2024.04.26 |