资讯专栏INFORMATION COLUMN

[Leetcode]Longest Palindrome

beita / 1221人阅读

摘要:解题思路我们发现结果其实就是字符的偶数个数是否有单一的字符,如果有就加把单一字符放在回文中间,如果没有就加字母区分大小写,代码

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:
7

Explanation:
One longest palindrome that can be built is "dccaccd", whose length is 7.

解题思路

我们发现结果其实就是字符的偶数个数+是否有单一的字符,如果有就加1(把单一字符放在回文中间),如果没有就加0;
字母区分大小写,int[] map=new int["z"-"A"+1];
2.代码

public class Solution {
    public int longestPalindrome(String s) {
        if(s.length()==0||s==null) return 0;
        int[] map=new int["z"-"A"+1];
        int count=0;
        int hasOdd=0;
        for(int i=0;i           
               
                                           
                       
                 

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/69828.html

相关文章

  • leetcode409.Longest Palindrome

    摘要:题目要求输入一个字符串,计算用这个字符串中的值构成一个最长回数的长度是多少。直观来看,我们立刻就能想到统计字符串中每个字符出现的次数,如果该字符出现次数为偶数,则字符一定存在于回数中。这个细节需要注意。 题目要求 Given a string which consists of lowercase or uppercase letters, find the length of the...

    linkin 评论0 收藏0
  • [LintCode/LeetCode] Longest Palindrome Substring

    摘要:是左闭右开区间,所以要。,要理解是和之间只有一个元素。循环每次的时候,都要更新子串更大的情况。补一种中点延展的方法循环字符串的每个字符,以该字符为中心,若两边为回文,则向两边继续延展。循环返回长度最长的回文串即可。 Problem Given a string S, find the longest palindromic substring in S. You may assume ...

    AaronYuan 评论0 收藏0
  • leetcode部分题目答案之JavaScript版

    摘要:自己没事刷的一些的题目,若有更好的解法,希望能够一起探讨项目地址 自己没事刷的一些LeetCode的题目,若有更好的解法,希望能够一起探讨 Number Problem Solution Difficulty 204 Count Primes JavaScript Easy 202 Happy Number JavaScript Easy 190 Reverse Bi...

    alphahans 评论0 收藏0
  • 前端 | 每天一个 LeetCode

    摘要:在线网站地址我的微信公众号完整题目列表从年月日起,每天更新一题,顺序从易到难,目前已更新个题。这是项目地址欢迎一起交流学习。 这篇文章记录我练习的 LeetCode 题目,语言 JavaScript。 在线网站:https://cattle.w3fun.com GitHub 地址:https://github.com/swpuLeo/ca...我的微信公众号: showImg(htt...

    张汉庆 评论0 收藏0
  • LeetCode 攻略 - 2019 年 7 月下半月汇总(100 题攻略)

    摘要:月下半旬攻略道题,目前已攻略题。目前简单难度攻略已经到题,所以后面会调整自己,在刷算法与数据结构的同时,攻略中等难度的题目。 Create by jsliang on 2019-07-30 16:15:37 Recently revised in 2019-07-30 17:04:20 7 月下半旬攻略 45 道题,目前已攻略 100 题。 一 目录 不折腾的前端,和咸鱼有什么区别...

    tain335 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<