Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit64ad1bb

Browse files
committed
Added 1 solution
1 parent012e5cb commit64ad1bb

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

‎Medium/Pancake Sorting.java

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
classSolution {
2+
publicList<Integer>pancakeSort(int[]A) {
3+
List<Integer>ans =newArrayList<>();
4+
int[]copy =Arrays.copyOf(A,A.length);
5+
intn =A.length -1;
6+
Arrays.sort(A);
7+
intgreatest =A.length -1;
8+
for (inti =0;i <A.length;i++) {
9+
intidx =findIdx(copy,A[greatest]);
10+
flip(copy,idx);
11+
ans.add(idx +1);
12+
flip(copy,greatest);
13+
ans.add(greatest +1);
14+
greatest--;
15+
}
16+
17+
returnans;
18+
}
19+
20+
privatevoidflip (int[]copy,intidx) {
21+
intstart =0;
22+
intend =idx;
23+
while (start <end) {
24+
inttemp =copy[start];
25+
copy[start] =copy[end];
26+
copy[end] =temp;
27+
start++;
28+
end--;
29+
}
30+
}
31+
32+
privateintfindIdx (int[]arr,inttarget) {
33+
for (inti =0;i <arr.length;i++) {
34+
if (arr[i] ==target) {
35+
returni;
36+
}
37+
}
38+
39+
return -1;
40+
}
41+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp