博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
409. Longest Palindrome
阅读量:5923 次
发布时间:2019-06-19

本文共 1056 字,大约阅读时间需要 3 分钟。

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.

难度:easy

题目:给定包含大小写字符组成的字符串,找出用这些字符串所能够成的最长回文串。

思路:字符统计

Runtime: 5 ms, faster than 90.61% of Java online submissions for Longest Palindrome.

Memory Usage: 34.7 MB, less than 100.00% of Java online submissions for Longest Palindrome.

class Solution {    public int longestPalindrome(String s) {        int[] table = new int[255];        for (char c: s.toCharArray()) {            table[c]++;        }        int length = 0, odd = 0;        for (int i = 0; i < 255; i++) {            if (table[i] % 2 > 0) {                odd = 1;            }                        length += table[i] - table[i] % 2;        }                return length + odd;    }}

转载地址:http://vwsvx.baihongyu.com/

你可能感兴趣的文章
Window 查看 端口操作
查看>>
Idea报错Command line is too long
查看>>
CSS书写规范
查看>>
Chapter 6. ListBox控件(双击播放图片)
查看>>
[BZOJ 2716][Violet 3]天使玩偶(CDQ分治+树状数组)
查看>>
[Codeforces Round #261 (Div. 2) E]Pashmak and Graph(Dp)
查看>>
PHP5.5新特性
查看>>
url_for和redirect区别
查看>>
BZOJ3524: [Poi2014]Couriers
查看>>
2018-2019-1 20165232 20165231 20165235实验二——固件程序设计
查看>>
判断Excel版本信息
查看>>
删除远程库文件
查看>>
go语言中的并发
查看>>
Python--介绍、变量、流程控制
查看>>
iptables-save 输出格式详解
查看>>
golang 读书笔记
查看>>
关于activity的启动模式
查看>>
体绘制(Volume Rendering)概述之3:光线投射算法(Ray Casting)原理和注意要点(强烈推荐呀,讲的很好)...
查看>>
Python--matplotlib绘图可视化知识点整理
查看>>
HDU 1058 Humble Numbers
查看>>