b161: Stack Sorting
Tags : sort stack
Accepted rate : 4人/18人 ( 22% ) [非即時]
評分方式:
Tolerant

最近更新 : 2024-01-29 11:52

Content

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.

Input

The data will be serialized to n1 , n2 , n3 , n4 ... , the last element is the element on the top of the stack.

Output

Output the Sorted stack. 

The last element is the element on the top of the stack.

Sample Input #1
4 2 1 3
Sample Output #1
1 2 3 4
Sample Input #2
4 1 5 2 6 3
Sample Output #2
1 2 3 4 5 6
測資資訊:
記憶體限制: 24 MB
不公開 測資點#0 (1%): 0.1s , <1K
不公開 測資點#1 (1%): 0.1s , <1K
不公開 測資點#2 (98%): 0.1s , <1M
Hint :
Tags:
sort stack
出處:
IBM interview question: how to sort a stack? [管理者:
louis@g.puic... (盧聖生Louis)
]


ID User Problem Subject Hit Post Date
沒有發現任何「解題報告」