[java]프로그래머스-같은 숫자는 싫어

corncheese·2024년 2월 3일
0

java알고리즘

목록 보기
1/2
public static ArrayList<Integer> solution( int[] arr){
        ArrayList<Integer> answer = new ArrayList<>();
        Stack<Integer> st = new Stack<>();

        for(int i=0; i<arr.length; i++){
            if(!st.empty() && st.peek() == arr[i]) st.pop();
            st.push(arr[i]);
        }

        for(int s : st) answer.add(s);

        return answer;
    }

0개의 댓글