ArrayList Pair Elimination


Submit solution

Points: 10
Time limit: 1.0s
Memory limit: 10M

Authors:
Problem type
Allowed languages
Java 19

題目說明

給定 n 個整數,請依序加入一個 ArrayList

接著從左到右檢查 ArrayList 中的元素:
相鄰兩個數字相同,則將這兩個數字一起刪除。
刪除後可能會產生新的相鄰相同數字,因此需持續檢查,直到 ArrayList 中不再存在相鄰且相同的數字為止。

最後輸出剩餘的數字。
若所有元素皆被刪除,請輸出 empty

提示:可使用 add()get()remove()size()ArrayList 方法。


Input / Output

類型 說明
輸入 第一行為整數 n,第二行為 n 個整數,以空格分隔
輸出 輸出剩餘數字,以空格分隔,若所有元素被刪除,輸出 empty

Sample 1 Sample 2
Input 8 /n 1 2 2 3 3 2 2 1 7 /n 1 3 3 2 4 4 5
Output empty 1 2 5

Comments

There are no comments at the moment.