zoukankan      html  css  js  c++  java
  • 回文字符串系列问题

    【leetcode】409. 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 class Solution {
     2 public:
     3     int longestPalindrome(string s) {
     4         int arr[52]={0};
     5         for(int i=0;i<s.size();i++)
     6         {
     7             if(s[i]>='A'&&s[i]<='Z')
     8                 arr[s[i]-'A'+26]++;
     9             else
    10                 arr[s[i]-'a']++;
    11         }
    12         int ret=0;
    13         int hasOdd=0;
    14         for(int i=0;i<52;i++)
    15         {
    16             if(arr[i]%2==0)
    17                 ret+=arr[i];
    18             else
    19             {
    20                 ret+=arr[i]-1;
    21                 hasOdd=1;
    22             }
    23         }
    24         return ret+hasOdd;
    25     }
    26 };
     
  • 相关阅读:
    接口
    多态
    封装
    初识继承
    对象的行为
    类、对象、包
    Java方法
    winform 报表的基本使用
    oracle配合C#的使用
    sql面试语句与后台调用js提示语句
  • 原文地址:https://www.cnblogs.com/SarahLiu/p/5999416.html
Copyright © 2011-2022 走看看