• 카테고리

    질문 & 답변
  • 세부 분야

    알고리즘 · 자료구조

  • 해결 여부

    미해결

궁금한게생겼습니다.

23.04.04 21:42 작성 조회수 247

0

공부하다가 보니 원소의 개 수 말고 원소를 뽑아내려고 하는데

public int solution(int n, int[] arr) {
        int answer = 1, max = arr[0];

        for (int i = 1; i < n; i++) {
            if (arr[i] > max) {
                answer = arr[i];
                i++;
                System.out.print(answer + " ");
            }
        }
        return answer;
    }

이렇게 해도 이상하지는 않을까요?

답변 1

답변을 작성해보세요.

0

안녕하세요^^

보이는 학생 자체를 배열에 담아 반환하는 프로그램을 작성해 봤습니다.

class Solution {
	public ArrayList<Integer> solution(int n, int[] arr) {
		ArrayList<Integer> answer = new ArrayList<>();
		int max = arr[0];
		answer.add(arr[0]);
                for (int i = 1; i < n; i++) {
                     if (arr[i] > max) {
                           answer.add(arr[i]);
                           max = arr[i];
                      }
                }
                return answer;
        }

        public static void main(String[] args){
	      Solution T = new Solution();
	      System.out.println(T.solution(8, new int[]{130, 135, 148, 140, 145, 150, 150, 153}));	
	}
}