409. Longest Palindrome
来源:互联网 发布:mac虚拟机安装教程 编辑:程序博客网 时间:2024/05/16 18:29
原题网址:https://leetcode.com/problems/longest-palindrome/
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.
方法:直方图频率统计。
public class Solution { public int longestPalindrome(String s) { char[] sa = s.toCharArray(); int[] frequency = new int[52]; for(char ch : sa) { if ('A' <= ch && ch <= 'Z') { frequency[ch - 'A'] ++; } else { frequency[ch - 'a' + 26] ++; } } boolean single = false; int len = 0; for(int f : frequency) { single |= (f & 1) == 1; len += f / 2; } return len * 2 + (single ? 1 : 0); }}
0 0
- 【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
- 11
- Scrum Master Mock Test (2)
- 网络协议概述
- Scrum Master Mock Test (3)
- Android应用耗电问题排查
- 409. Longest Palindrome
- 网站重定向中301与302的区别(精简)
- 德国人二战后自己重写历史(图)
- Python zip()使用方法
- CSM认证部分题目分析(1)
- 1111122211
- [技巧]枚举子集的飘逸写法
- 一次性能峰值提升10W的DB调优之旅
- 求黄金比例golden ratio近似值