?LeetCode刷題實(shí)戰(zhàn)409:最長回文串
Given a string s which consists of lowercase or uppercase letters, return the length of the longest palindrome that can be built with those letters.
Letters are case sensitive, for example, "Aa" is not considered a palindrome here.
示例
輸入:
"abccccdd"
輸出:
7
解釋:
我們可以構(gòu)造的最長的回文串是"dccaccd", 它的長度是 7。
解題
class?Solution?{
????public?int?longestPalindrome(String s)?{
????????int[] count = new?int[58];
????????for?(int?i=0;i????????????count[s.charAt(i)-'A']++;
????????int?ans = 0;
????????for?(int?j:count)
????????????ans+=(j/2)*2;
????????if?(ans < s.length())
????????????ans++;
????????return?ans;
????}
}
LeetCode1-400題匯總,希望對你有點(diǎn)幫助!
LeetCode刷題實(shí)戰(zhàn)401:二進(jìn)制手表
LeetCode刷題實(shí)戰(zhàn)402:移掉 K 位數(shù)字
LeetCode刷題實(shí)戰(zhàn)403:青蛙過河
LeetCode刷題實(shí)戰(zhàn)404:左葉子之和
LeetCode刷題實(shí)戰(zhàn)405:數(shù)字轉(zhuǎn)換為十六進(jìn)制數(shù)
LeetCode刷題實(shí)戰(zhàn)406:根據(jù)身高重建隊(duì)列
LeetCode刷題實(shí)戰(zhàn)407:接雨水 II
LeetCode刷題實(shí)戰(zhàn)408:有效單詞縮寫
