New Problem Solution "Longest Palindrome"
This commit is contained in:
parent
7159e86637
commit
33624b303a
@ -12,6 +12,7 @@ LeetCode
|
|||||||
|414|[Third Maximum Number](https://leetcode.com/problems/third-maximum-number/) | [C++](./algorithms/cpp/thirdMaximumNumber/ThirdMaximumNumber.cpp)|Easy|
|
|414|[Third Maximum Number](https://leetcode.com/problems/third-maximum-number/) | [C++](./algorithms/cpp/thirdMaximumNumber/ThirdMaximumNumber.cpp)|Easy|
|
||||||
|413|[Arithmetic Slices](https://leetcode.com/problems/arithmetic-slices/) | [C++](./algorithms/cpp/arithmeticSlices/ArithmeticSlices.cpp)|Medium|
|
|413|[Arithmetic Slices](https://leetcode.com/problems/arithmetic-slices/) | [C++](./algorithms/cpp/arithmeticSlices/ArithmeticSlices.cpp)|Medium|
|
||||||
|412|[Fizz Buzz](https://leetcode.com/problems/fizz-buzz/) | [C++](./algorithms/cpp/fizzBuzz/FizzBuzz.cpp)|Easy|
|
|412|[Fizz Buzz](https://leetcode.com/problems/fizz-buzz/) | [C++](./algorithms/cpp/fizzBuzz/FizzBuzz.cpp)|Easy|
|
||||||
|
|409|[Longest Palindrome](https://leetcode.com/problems/longest-palindrome/) | [C++](./algorithms/cpp/longestPalindrome/LongestPalindrome.cpp)|Easy|
|
||||||
|406|[Queue Reconstruction by Height](https://leetcode.com/problems/queue-reconstruction-by-height/) | [C++](./algorithms/cpp/queueReconstructionByHeight/QueueReconstructionByHeight.cpp)|Medium|
|
|406|[Queue Reconstruction by Height](https://leetcode.com/problems/queue-reconstruction-by-height/) | [C++](./algorithms/cpp/queueReconstructionByHeight/QueueReconstructionByHeight.cpp)|Medium|
|
||||||
|405|[Convert a Number to Hexadecimal](https://leetcode.com/problems/convert-a-number-to-hexadecimal/) | [C++](./algorithms/cpp/convertANumberToHexadecimal/ConvertANumberToHexadecimal.cpp)|Easy|
|
|405|[Convert a Number to Hexadecimal](https://leetcode.com/problems/convert-a-number-to-hexadecimal/) | [C++](./algorithms/cpp/convertANumberToHexadecimal/ConvertANumberToHexadecimal.cpp)|Easy|
|
||||||
|404|[Sum of Left Leaves](https://leetcode.com/problems/sum-of-left-leaves/) | [C++](./algorithms/cpp/sumOfLeftLeaves/SumOfLeftLeaves.cpp)|Easy|
|
|404|[Sum of Left Leaves](https://leetcode.com/problems/sum-of-left-leaves/) | [C++](./algorithms/cpp/sumOfLeftLeaves/SumOfLeftLeaves.cpp)|Easy|
|
||||||
|
50
algorithms/cpp/longestPalindrome/LongestPalindrome.cpp
Normal file
50
algorithms/cpp/longestPalindrome/LongestPalindrome.cpp
Normal file
@ -0,0 +1,50 @@
|
|||||||
|
// Source : https://leetcode.com/problems/longest-palindrome/
|
||||||
|
// Author : Hao Chen
|
||||||
|
// Date : 2016-11-13
|
||||||
|
|
||||||
|
/***************************************************************************************
|
||||||
|
*
|
||||||
|
* Given a string which consists of lowercase or uppercase letters, find the length of
|
||||||
|
* the longest palindromes that can be built with those letters.
|
||||||
|
*
|
||||||
|
* 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.
|
||||||
|
***************************************************************************************/
|
||||||
|
|
||||||
|
class Solution {
|
||||||
|
public:
|
||||||
|
int longestPalindrome(string s) {
|
||||||
|
|
||||||
|
int hashtable[128];
|
||||||
|
memset(hashtable, 0, sizeof(hashtable));
|
||||||
|
|
||||||
|
for(char ch : s) {
|
||||||
|
hashtable[ch]++;
|
||||||
|
}
|
||||||
|
int result = 0;
|
||||||
|
bool hasOdd = false;
|
||||||
|
for (int n : hashtable) {
|
||||||
|
if ( n%2 == 0 ) {
|
||||||
|
result += n;
|
||||||
|
} else {
|
||||||
|
result += n -1;
|
||||||
|
hasOdd = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return hasOdd ? result + 1 : result;
|
||||||
|
}
|
||||||
|
};
|
Loading…
x
Reference in New Issue
Block a user