#P1160. Stack Sorting

    ID: 1160 Type: Default 100ms 24MiB Tried: 0 Accepted: 0 Difficulty: 1 Uploaded By: Tags>BASICsortstackIBM interview question: how to sort a stack?

Stack Sorting

題目描述

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