forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.ts
41 lines (39 loc) · 1.03 KB
/
Solution.ts
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
function largestPalindromic(num: string): string {
const count = new Array(10).fill(0);
for (const c of num) {
count[c]++;
}
while (count.reduce((r, v) => (v % 2 === 1 ? r + 1 : r), 0) > 1) {
for (let i = 0; i < 10; i++) {
if (count[i] % 2 === 1) {
count[i]--;
break;
}
}
}
let res = [];
let oddIndex = -1;
for (let i = 9; i >= 0; i--) {
if (count[i] % 2 == 1) {
oddIndex = i;
count[i] -= 1;
}
res.push(...new Array(count[i] >> 1).fill(i));
}
if (oddIndex !== -1) {
res.push(oddIndex);
}
const n = res.length;
for (let i = 0; i < n; i++) {
if (res[i] !== 0) {
res = res.slice(i);
if (oddIndex !== -1) {
res.push(...[...res.slice(0, res.length - 1)].reverse());
} else {
res.push(...[...res].reverse());
}
return res.join('');
}
}
return '0';
}