sort numbers with three stacks

Posted davidnyc

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了sort numbers with three stacks相关的知识,希望对你有一定的参考价值。

s3 是用来存放sort 结果的,整个算法非常像 selection sort/bubble sort

1: 每次从s1中选出最小的一个放在s3 中,其他非最小的放入s2 中

2: 然后把s2 的数字放回到s1 中

循环结束后,s3 中的就是sort 好的结果

 1 public void sort(LinkedList<Integer> s1) {
 2     Deque<Integer> s2 = new LinkedList<>();
 3     Deque<Integer> s3 = new LinkedList<>();
 4     
 5     int size = s1.size(); 
 6     for (int i = 0; i < size; i++) {
 7         int globMin = Integer.MAX_VALUE;
 8         while (!s1.isEmpty()) {
 9             int top = s1.pop();
10             globMin = Math.min(globMin, top);
11             s2.push(top);
12         }
13 
14         s3.push(globMin);
15         while (!s2.isEmpty()) {
16 int top = s2.pop();
17 if (top == globMin) {
18 globMin = Integer.MAX_VALUE;
19 continue;    
20 }
21 s1.push(top);
22         }
23     }
24     
25     while (!s3.isEmpty()) {
26         s1.push(s3.pop());
27     }
28 }

 

以上是关于sort numbers with three stacks的主要内容,如果未能解决你的问题,请参考以下文章

1023 Have Fun with Numbers (20)(20 point(s))

[LeetCode] 1513. Number of Substrings With Only 1s

1513. Number of Substrings With Only 1s

Have fun with numbers

容器化开发_04_calico报错(Number of node(s) with BGP peering established = 0)

LeetCode 1213. Intersection of Three Sorted Arrays