leetcode409.Longest Palindrome
生活随笔
收集整理的這篇文章主要介紹了
leetcode409.Longest Palindrome
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
題目要求
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: 7Explanation: One longest palindrome that can be built is "dccaccd", whose length is 7.輸入一個字符串,計算用這個字符串中的值構成一個最長回數(shù)的長度是多少。
思路和代碼
這是一道easy難度的題目,但是一次性寫對也有挑戰(zhàn)。直觀來看,我們立刻就能想到統(tǒng)計字符串中每個字符出現(xiàn)的次數(shù),如果該字符出現(xiàn)次數(shù)為偶數(shù),則字符一定存在于回數(shù)中。但是我們忽略了一點,即如果字符中存在一個額外的單個字符位于中間,該字符串也能構成回數(shù),如aabaa。這個細節(jié)需要注意。
下面是O(N)時間的實現(xiàn):
總結
以上是生活随笔為你收集整理的leetcode409.Longest Palindrome的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: LeetCode40.组合总和|| Ja
- 下一篇: 二进制安装部署 4 kubernete