-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProblem_2384_largestPalindromic.cc
70 lines (68 loc) · 1.47 KB
/
Problem_2384_largestPalindromic.cc
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
58
59
60
61
62
63
64
65
66
67
68
69
70
#include <string>
#include <vector>
using namespace std;
class Solution
{
public:
string largestPalindromic(string num)
{
int n = num.size();
// '0' ~ '9'字符对应整数为48 ~ 57
vector<int> cnts(58);
for (char c : num)
{
cnts[c]++;
}
string ans(n, ' ');
int leftSize = 0;
char mid = 0;
for (char i = '9'; i >= '1'; i--)
{
if ((cnts[i] & 1) && mid == 0)
{
// 记录最大出现奇数次的数字
mid = i;
}
for (int j = cnts[i] / 2; j > 0; j--)
{
// 左边从大到小依次填好
ans[leftSize++] = i;
}
}
// '1'~'9'每一种字符出现的次数 <= 1
if (leftSize == 0)
{
if (mid == 0)
{
// '1'~'9'每一种字符出现的次数 == 0
return "0";
}
else
{
// '1'~'9' 有若干字符出现次数 == 1,其中最大的字符是mid
return std::string(1, mid);
}
}
// '1'~'9'有若干字符出现次数 >= 2,左部分已经建立,再考虑把'0'字符填进来
for (int j = cnts['0'] / 2; j > 0; j--)
{
ans[leftSize++] = '0';
}
int len = leftSize;
// 把中点安插进去
if (mid == 0 && (cnts['0'] & 1) == 1)
{
mid = '0';
}
if (mid != 0)
{
ans[len++] = mid;
}
// 左部分逆序拷贝给右部分
for (int i = leftSize - 1; i >= 0; i--)
{
ans[len++] = ans[i];
}
return ans.substr(0, len);
}
};