題目描述
Sort a stack in ascending order (with biggest terms on top).
You may use at most one additional stack to hold items, but you may not copy the elements into any other data structure (e.g. array).
Example:
Given stack =
| |
|3|
|1|
|2|
|4|
---
Return:
| |
|4|
|3|
|2|
|1|
---
The data will be serialized to [4,2,1,3]. The last element is the element on the top of the stack.
Challenge
O(n^2) time is acceptable.
輸入格式
The data will be serialized to n1 , n2 , n3 , n4 ... , the last element is the element on the top of the stack.
輸出格式
Output the Sorted stack.
The last element is the element on the top of the stack.
Samples
["4 2 1 3","4 1 5 2 6 3"]
["1 2 3 4","1 2 3 4 5 6"]
原始資料
- Zero1 題號:
b161
- Hydro 題號:
Z1161
- Locale:
zh_TW
- Display:
open