594.Peek at the Top After Each Operation
Easy
Process
ops on an initially empty stack: a positive number means push it, and 0 means pop (ignored if the stack is empty). After every operation, record the value on top of the stack, or −1 if it is empty.
PeekPeek (Top)Looking at the item on top of a stack without removing it. reads the top item without removing it. It is how many algorithms "look before they act" — for example, checking the most recent opening bracket before deciding whether a closing bracket matches. Like pop, peek must handle the empty case.
Example 1:
Input: ops = [4,9,0,0,0,6]
Output: [4,9,4,-1,-1,6]
Example 2:
Input: ops = [0]
Output: [-1]
+ 4 hidden test cases run on Submit.
Constraints:
- ●
1 ≤ ops.length ≤ 1000 - ●
0 ≤ ops[i] ≤ 1000000 (a positive value means push it, 0 means pop)
ops =
[4, 9, 0, 0, 0, 6]