-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNo969PancakeSorting.java
57 lines (51 loc) · 1.17 KB
/
No969PancakeSorting.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package com.wzx.leetcode;
import java.util.LinkedList;
import java.util.List;
/**
* @see <a href="https://leetcode.com/problems/pancake-sorting/">https://leetcode.com/problems/pancake-sorting/</a>
* @author wzx
*/
public class No969PancakeSorting {
/**
* 递归
* <p>
* time: O(n^2)
* space: O(1)
*/
public List<Integer> pancakeSort(int[] arr) {
List<Integer> res = new LinkedList<>();
recursion(arr, arr.length, res);
return res;
}
/**
* 翻转两次使最大的煎饼处在k层
*/
private void recursion(int[] arr, int k, List<Integer> res) {
if (k == 1) return;
// 找到最大煎饼
int maxIndex = 0, maxValue = 0;
for (int i = 1; i <= k; i++) {
if (arr[i - 1] > maxValue) {
maxIndex = i;
maxValue = arr[i - 1];
}
}
// 把最大煎饼翻到第1层
reverse(arr, maxIndex);
res.add(maxIndex);
// 再整个翻下来
reverse(arr, k);
res.add(k);
recursion(arr, k - 1, res);
}
private void reverse(int[] arr, int k) {
int i = 0, j = k - 1;
while (i < j) {
int tmp = arr[i];
arr[i] = arr[j];
arr[j] = tmp;
i++;
j--;
}
}
}