기초 공부 (언어 및 알고리즘)/알고리즘 (Java)

[프로그래머스/Java] 같은 숫자는 싫어(스택/큐) 풀이

iinana 2025. 3. 6. 07:31
728x90

프로그래머스 알고리즘 고득점 Kit의 스택&큐 문제 중 하나인 '같은 숫자는 싫어'를 자바로 풀어보았다. 

https://school.programmers.co.kr/learn/courses/30/lessons/12906

 

프로그래머스

SW개발자를 위한 평가, 교육, 채용까지 Total Solution을 제공하는 개발자 성장을 위한 베이스캠프

programmers.co.kr

 

 

import java.util.*;

public class Solution {
    public int[] solution(int []arr) {
        List<Integer> answer = new ArrayList<Integer>();
        int pre = -1;
        for (int num : arr) {
            if (num == pre) continue;
            pre = num;
            answer.add(num);
        }
        return answer.stream()
            .mapToInt(i -> (int)i).toArray();
    }
}
728x90