593.Pop Items from a Stack
Easy
A stack is given as an array from bottom to top. Pop up to
k items from it and return the popped values in the order they came off. If the stack runs empty first, stop.
PopPopRemoving and returning the item at the top of a stack. removes from the top — the most recently added item — which is the LIFOLIFO (Last In, First Out)The rule that the most recently added item is the first one removed. rule. Popping an empty stack is called stack underflowStack UnderflowTrying to pop from a stack that has no items., and every pop must guard against it.
Example 1:
Input: stack = [3,8,5], k = 2
Output: [5,8]
Example 2:
Input: stack = [3,8], k = 4
Output: [8,3]
+ 4 hidden test cases run on Submit.
Constraints:
- ●
0 ≤ stack.length ≤ 1000 (given bottom to top) - ●
1 ≤ k ≤ 1000 - ●
-1000000 ≤ stack[i] ≤ 1000000
stack =
[3, 8, 5]
k =
2