题目:

  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:

  1. Input:
  2. "abccccdd"
  3.  
  4. Output:
  5.  
  6. Explanation:
  7. One longest palindrome that can be built is "dccaccd", whose length is 7.

大意:

  给出一个由小写或大写字母组成的字符串,找到能被其中的字母组成的最长的回文的长度。

  这是区分大小写的,比如“Aa”就不能认为是回文。

  注意: 
  假设给出的字符串长度不会超过1010。

  例子:

  1. 输入:
  2. abccccdd
  3.  
  4. 输出:
  5.  
  6. 解释:
  7. 最长的回文为“dccaccd”,长度为7

  回文就是中间有一个单个的字母,两边的字母是对称的。

  • aaadd的最长回文是daaad
  • aaadddcc的最长回文是cdaaadc或者cadddac

  可以总结为:

  • 数目为偶数的字母将全部出现在最长回文中,
  • 奇数就有两种情况,有没有大于1个的,如果有大于1个的。就比如有3个,那么回文长度将加上2。如果有7个就加上6.
  • 如果所有的奇数都不大于1,最后结果再加1即可
  1. class Solution {
  2. public int longestPalindrome(String s) {
  3. int result = 0;
  4. boolean flag = false;
  5. int[] a = new int[26*2];
  6. for (int i = 0; i < s.length(); i++) {
  7. if (s.charAt(i) < 'a') {
  8. a[s.charAt(i) - 'A' + 26]++;
  9. } else {
  10. a[s.charAt(i) - 'a'] ++;
  11. }
  12. }
  13. for(int i:a){
  14. if(i == 1){
  15. flag = true;
  16. }
  17. if(i > 1){
  18. result += i / 2 * 2;
  19. if (i % 2 == 1) flag = true;
  20. }
  21. }
  22. if(flag){
  23. result++;
  24. }
  25. return result;
  26. }
    }

  这里使用一个26*2的数组记录字幕个数,因为是区分大小写的,每次如果字母数目大于1先除2再乘2,这样偶数不变,奇数将减一。还设置了一个flag变量判断是否存在奇数。

LeetCode——409. Longest Palindrome的更多相关文章

  1. 24. leetcode 409. Longest Palindrome

    409. Longest Palindrome Given a string which consists of lowercase or uppercase letters, find the le ...

  2. LeetCode 409. Longest Palindrome (最长回文)

    Given a string which consists of lowercase or uppercase letters, find the length of the longest pali ...

  3. [LeetCode] 409. Longest Palindrome 最长回文

    Given a string which consists of lowercase or uppercase letters, find the length of the longest pali ...

  4. LeetCode 409 Longest Palindrome

    Problem: Given a string which consists of lowercase or uppercase letters, find the length of the lon ...

  5. 【leetcode】409. Longest Palindrome

    problem 409. Longest Palindrome solution1: class Solution { public: int longestPalindrome(string s) ...

  6. 【LeetCode】409. Longest Palindrome 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:字典统计次数 方法二:HashSet 方法三 ...

  7. [LeetCode&Python] Problem 409. Longest Palindrome

    Given a string which consists of lowercase or uppercase letters, find the length of the longest pali ...

  8. 409. Longest Palindrome

    Given a string which consists of lowercase or uppercase letters, find the length of the longest pali ...

  9. 409. Longest Palindrome 最长对称串

    [抄题]: Given a string which consists of lowercase or uppercase letters, find the length of the longes ...

随机推荐

  1. LinkedHashMap如何保证顺序性

    一. 前言 先看一个例子,我们想在页面展示一周内的消费变化情况,用echarts面积图进行展示.如下: 我们在后台将数据构造完成 HashMap<String, Integer> map ...

  2. Java面试题汇总---整理版(附答案)

    今天继续为大家整理Java面试题,并涉及数据库和网络等相关知识,希望能帮助到各位开发者. 1,为什么要用spring,Spring主要使用了什么模式? spring能够很好的和各大框架整合,它通过IO ...

  3. bulk更新mongodb的脚本

    bulk批处理mongodb,比普通的js脚本来的更快一些. 官方网址:https://docs.mongodb.com/manual/reference/method/Bulk/ bulk支持的方法 ...

  4. Android/AndroidStudio/idea使用教程之git使用(详细)(码云)

    已经安装好了AndroidStudio,安装教程 本教程是作者自己摸索出来的,有不足之处还请大家海涵.多多拍砖,互相学习. 第一步:下载git,安装git客户端 直接百度git,下载git ​ 安装g ...

  5. php上传excle文件,csv文件解析为二维数组

    解析上传的CSV文件不是什么难事,直接读取转成你想要的数组样子就OK了. public function putStoreStockIn ($filePath = '') { $file = fope ...

  6. TP框架基础(四)----添加数据

    [数据添加] add() 该方法返回被添加的新记录的主键id值 两种方式实现数据添加 1. 数组方式数据添加 $goods = D(“Goods”); $arr = array(‘goods_name ...

  7. linux初学者-虚拟机联网篇

    linux初学者-虚拟机联网篇 在虚拟机的使用过程中,本机可以连接WIFI直接上网,但是有时候需要用到虚拟机的联网,那么在本机联网的情况下,虚拟机怎么联网呢?接下来将介绍如何在本机已经连接到WIFI的 ...

  8. fuel 9.0完全离线配置+升级fule 9.1+bootstrap制作

    2017-08-23 fuel 完全离线配置 完全离线适用于部署在内网,无Internet的环境当中,且需要升级fuel版本的情况.理论上,我的办法适用所有版本. 以下所有操作全部在fuel节点上. ...

  9. Centos7安装mysql8教程

    网上的教程很多,我也参考了很多,以下是我实践的步骤,真实有效. 1.配置Mysql 8.0安装源: sudo rpm -Uvh https://dev.mysql.com/get/mysql80-co ...

  10. 动态规划_Apple Catching_POJ-2385

    It and ) in his field, each full of apples. Bessie cannot reach the apples when they are on the tree ...