Merge Equal Neighbors

Imagine a tile game: when two neighboring tiles are equal, the left tile doubles and the right tile becomes zero. Write a function apply_operations that scans nums from left to right and applies this rule to each neighboring pair. After all operations, move every zero to the end while keeping the order of the other numbers. Return the resulting list.

Примеры

Ввод
[1, 2, 2, 1, 1, 0]
Вывод
[1, 4, 2, 0, 0, 0]
Ввод
[0, 1]
Вывод
[1, 0]
Ввод
[1, 1, 1]
Вывод
[2, 1, 0]

Нужно реализовать

def apply_operations(nums: list[int]) -> list[int]:

Теги

arraytwo-pointers
PostgreSQLv16

Qui apparirà il risultato dell'esecuzione del codice

Visible tests
1
Input
[1,2,2,1,1,0]
Expected[1,4,2,0,0,0]
2
Input
[0,1]
Expected[1,0]
3
Input
[1,1,1]
Expected[2,1,0]
4
Input
[2,2,2,2]
Expected[4,4,0,0]
5
Input
[5]
Expected[5]
6
Input
[0,0,0]
Expected[0,0,0]
Focus radio
Paused · SomaFM · Fluid