Algorithm/Programmers
[Programmers][Java] 가장 큰 수 찾기
j4ko
2024. 8. 12. 22:55
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
반응형