Skip to content

Commit 79b0d33

Browse files
committed
feat: add typescript solution to lc problem: No.0969
No.0969.Pancake Sorting
1 parent a0f259a commit 79b0d33

File tree

3 files changed

+79
-0
lines changed

3 files changed

+79
-0
lines changed

solution/0900-0999/0969.Pancake Sorting/README.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,34 @@
7777

7878
```
7979

80+
### **TypeScript**
81+
82+
```ts
83+
function pancakeSort(arr: number[]): number[] {
84+
let ans = [];
85+
for (let n = arr.length; n > 1; n--) {
86+
let index = 0;
87+
for (let i = 1; i < n; i++) {
88+
if (arr[i] >= arr[index]) {
89+
index = i;
90+
}
91+
}
92+
if (index == n - 1) continue;
93+
reverse(arr, index);
94+
reverse(arr, n - 1);
95+
ans.push(index + 1);
96+
ans.push(n);
97+
}
98+
return ans;
99+
};
100+
101+
function reverse (nums: Array<number>, end: number): void {
102+
for (let i = 0, j = end; i < j; i++, j--) {
103+
[nums[i], nums[j]] = [nums[j], nums[i]];
104+
}
105+
}
106+
```
107+
80108
### **...**
81109

82110
```

solution/0900-0999/0969.Pancake Sorting/README_EN.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,34 @@ Note that other answers, such as [3, 3], would also be accepted.
6666

6767
```
6868

69+
### **TypeScript**
70+
71+
```ts
72+
function pancakeSort(arr: number[]): number[] {
73+
let ans = [];
74+
for (let n = arr.length; n > 1; n--) {
75+
let index = 0;
76+
for (let i = 1; i < n; i++) {
77+
if (arr[i] >= arr[index]) {
78+
index = i;
79+
}
80+
}
81+
if (index == n - 1) continue;
82+
reverse(arr, index);
83+
reverse(arr, n - 1);
84+
ans.push(index + 1);
85+
ans.push(n);
86+
}
87+
return ans;
88+
};
89+
90+
function reverse (nums: Array<number>, end: number): void {
91+
for (let i = 0, j = end; i < j; i++, j--) {
92+
[nums[i], nums[j]] = [nums[j], nums[i]];
93+
}
94+
}
95+
```
96+
6997
### **...**
7098

7199
```
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
function pancakeSort(arr: number[]): number[] {
2+
let ans = [];
3+
for (let n = arr.length; n > 1; n--) {
4+
let index = 0;
5+
for (let i = 1; i < n; i++) {
6+
if (arr[i] >= arr[index]) {
7+
index = i;
8+
}
9+
}
10+
if (index == n - 1) continue;
11+
reverse(arr, index);
12+
reverse(arr, n - 1);
13+
ans.push(index + 1);
14+
ans.push(n);
15+
}
16+
return ans;
17+
};
18+
19+
function reverse (nums: Array<number>, end: number): void {
20+
for (let i = 0, j = end; i < j; i++, j--) {
21+
[nums[i], nums[j]] = [nums[j], nums[i]];
22+
}
23+
}

0 commit comments

Comments
 (0)