-
Notifications
You must be signed in to change notification settings - Fork 135
Expand file tree
/
Copy path0969-PancakeSorting.cs
More file actions
42 lines (36 loc) · 1.06 KB
/
0969-PancakeSorting.cs
File metadata and controls
42 lines (36 loc) · 1.06 KB
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
//-----------------------------------------------------------------------------
// Runtime: 244ms
// Memory Usage: 30.9 MB
// Link: https://leetcode.com/submissions/detail/363023872/
//-----------------------------------------------------------------------------
using System.Collections.Generic;
namespace LeetCode
{
public class _0969_PancakeSorting
{
public IList<int> PancakeSort(int[] A)
{
var result = new List<int>();
for (int x = A.Length; x > 0; x--)
{
int index = 0;
while (A[index] != x) index++;
if (index + 1 == x) continue;
Flip(A, index + 1);
result.Add(index + 1);
Flip(A, x);
result.Add(x);
}
return result;
}
private void Flip(int[] a, int k)
{
for (int i = 0, j = k - 1; i < j; i++, j--)
{
var temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
}