lintCode627:最长回文串
来源:互联网 发布:尼尔森数据分析报告 编辑:程序博客网 时间:2024/06/05 21:03
public class Solution { /* * @param s: a string which consists of lowercase or uppercase letters * @return: the length of the longest palindromes that can be built */ public int longestPalindrome(String s) { // write your code here int size = 0 ; int max = 0; char[] charArr = s.toCharArray(); int count[] = new int[128]; //将所有字符串中的字符存入字符数组当中 for(int i=0;i<charArr.length;i++){ count[charArr[i]]++; } //遍历 for(int i=0;i<128;i++){ //由回文串的特点可知,为偶肯定可以当回文串元素 if(count[i]%2==0){ size += count[i]; } //判断,如果比记录的max大,则加入前max-1的子串长度,否则直接加入 else if(count[i]%2!=0&&count[i]>max){ if(max!=0) size = size + max -1; max = count[i]; }else{ size +=count[i]-1; } } return size+max; }}
阅读全文
0 0
- lintCode627:最长回文串
- 最长回文子串
- 最长回文子串
- 最长回文子串
- 最长回文子串
- 最长回文子串
- 最长回文子串
- 最长回文子串
- 最长回文子串
- 最长回文子串
- 最长回文子串
- 最长回文子串
- 最长回文子串
- 最长回文子串
- 最长回文子串
- 最长回文子串
- 最长回文串模版
- 最长回文子串
- Ubuntu SMP 16.04.1 个人配置习惯
- leetcode习题解答:6. ZigZag Conversion
- 判断32位整数二进制中1的个数的算法
- spring AOP 领悟
- A NEW DAY
- lintCode627:最长回文串
- eclipse 中安装activiti插件
- 【洛谷2024】食物链 并查集
- 【量化小讲堂-Python&Pandas系列07】数据告诉你:惊人的海龟交易法则
- 不修复软件缺陷的原因
- jquery获取自身元素的html
- C++模板,模板具体化,特例化
- 创建一个项目--[Angular入门]
- 6.4