409. Longest Palindrome

题目

https://leetcode.com/problems/longest-palindrome/description/

想法

easy级的题目,统计奇偶就可以了

答案

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
class Solution {
public:
int longestPalindrome(string s) {
vector<int> num(52);
for (char c : s) {
int i = c >= 'a' ? c - 'a' + 26 : c - 'A';
num[i]++;
}
int len = 0;
bool oddFlag = false;
for (int i = 0; i < num.size(); i++) {
if (num[i] % 2 == 0)
len += num[i];
else
if (oddFlag == false) {
len += num[i];
oddFlag = true;
} else {
len += num[i] - 1;
}
}
return len;
}
};

回顾

可以尝试用其它语言,如JS,Python,Swift等语言来解决问题来锻炼语言掌握的熟练度