409. Longest Palindrome
来源:互联网 发布:java上传txt文件 编辑:程序博客网 时间:2024/06/13 07:54
This is case sensitive, for example "Aa" is not considered a palindrome here.
Note:
Assume the length of given string will not exceed 1,010.
Example:
Input:
"abccccdd"
Output:
7
Explanation:
One longest palindrome that can be built is "dccaccd", whose length is 7.
Subscribe to see which companies asked this question.
#include <iostream>
#include <string>
using namespace std;
class Solution {
public:
int longestPalindrome(string s) {
int times[52]={0};
for(int i=0;i<s.length();i++)
{
if(s[i] >= 'A' && s[i] <= 'Z')
{
int n = s[i]-'A';
times[n]++;
}
else if(s[i] >= 'a' && s[i] <= 'z')
{
int n = s[i]-'a'+26;
times[n]++;
}
}
int count = 0;
int max = 0;
int index = 0;
for(int i=0;i<52;i++)
{
if(times[i] == 0)
count+=0;
else if(times[i]%2 == 0)
count+=times[i];
else
{
max = times[i];
index = i;
// cout << i << " " << index << endl;
count = count + times[index];
break;
}
// cout << count << endl;
}
if(index == 0 && max == 0)
return count;
else
{
for(int j=index+1;j<52;j++)
{
if(times[j] == 0)
count+=0;
else if(times[j]%2 == 0)
count+=times[j];
else
{
if(times[j] > max)
{
max = times[j];
count--;
count+=times[j];
}
else
{
times[j]--;
count+=times[j];
}
}
// cout << count << endl;
}
return count;
}
}
};
- 【Leetcode】409. Longest Palindrome
- 409. Longest Palindrome
- 409. Longest Palindrome
- [LeetCode]409. Longest Palindrome
- 409. Longest Palindrome
- 409. Longest Palindrome
- 409. Longest Palindrome
- LeetCode 409. Longest Palindrome
- 409. Longest Palindrome
- [LeetCode]--409. Longest Palindrome
- leetcode 409. Longest Palindrome
- LeetCode 409. Longest Palindrome
- 409. Longest Palindrome
- LeetCode 409. Longest Palindrome
- 409. Longest Palindrome
- 409. Longest Palindrome
- 409. Longest Palindrome
- 409. Longest Palindrome
- 【taotao】solr
- springboot微服务架构——搭建入门
- P
- 5G 时代的到来意味着什么?
- 访问者模式
- 409. Longest Palindrome
- 基于咸阳农业网与咸阳新兴纺织园网的URL分析
- k8s不能挂载ceph块存储
- iOS自定义控件教程(六)UIResponder Chain
- mongodb的安装和简单使用
- MFC获取窗口和客户区坐标分析
- R 语言下常用第三方库的说明
- iOS高薪面试题
- PCL三维点云拼接融合技术