【LeetCode】 String中的最长回文
java 普通版:
1.正序遍历数组,取得子字符串的首字母。
2.倒序遍历数组,取的子字符串的尾字母。
(这样仅仅要在子循环中第一个出现回文,那么该回文肯定是本次循环的最长的回文)
3.新增数据结构,存储出现最长的那个子串的长度,起始下标和结束下标。
/**
*
*/
package com.cxm; /**
* @author admin
*
*/
public class PalindromeS
{
private static String str = "hfjdjajdjhjlshlajdjajdjlsjdlsjiwowjvvmz.zjjfdkdfjjz.lafdiofeqnvkcajdlajiwonvbhdskalhdjfkda;jfdk;ajfdkjfkda;"; public static void main(String[] args)
{
PalindromeS PalindromeS = new PalindromeS();
PalindromeS.findpalindromeS();
} public void findpalindromeS(){
BigSotre bigSotre = PalindromeS.this.getBigSotre() ;
char[] charArray = str.toCharArray();
for(int i = 0;i<charArray.length;i++){
for(int j =charArray.length -1;j>i;j--){
if(isPalindrome(charArray,i,j)){
bigSotre.store(i, j);
break;
}
}
}
System.out.println("最大回文长度"+bigSotre.bigSzie+" 起始下标"+bigSotre.startIndex+" 结束下标 "+bigSotre.endIndex);
} private class BigSotre{
int bigSzie; int startIndex; int endIndex; BigSotre(int i,int j,int k){
this.bigSzie = i;
this.startIndex = j;
this.endIndex = k; } public BigSotre store(int i ,int j){
if((j-i+1)>this.bigSzie){
this.bigSzie = j-i+1;
this.startIndex = i;
this.endIndex = j;
}
return this;
} } public BigSotre getBigSotre(){
return new BigSotre(0,0,0);
} public boolean isPalindrome(char[] charArray,int i ,int j ){
int intL = j-i +1;
int length = (intL>>1)+i;
while(i<length){
if(charArray[i]!=charArray[j]){
return false;
}
i++;
j--;
}
return true;
}
}
【LeetCode】 String中的最长回文的更多相关文章
- LeetCode:Longest Palindromic Substring 最长回文子串
题目链接 Given a string S, find the longest palindromic substring in S. You may assume that the maximum ...
- 转载:LeetCode:5Longest Palindromic Substring 最长回文子串
本文转自:http://www.cnblogs.com/TenosDoIt/p/3675788.html 题目链接 Given a string S, find the longest palindr ...
- [LeetCode] Longest Palindromic Substring 最长回文串
Given a string S, find the longest palindromic substring in S. You may assume that the maximum lengt ...
- [LeetCode] Longest Palindromic Subsequence 最长回文子序列
Given a string s, find the longest palindromic subsequence's length in s. You may assume that the ma ...
- LeetCode之“字符串”:最长回文子串
题目要求: 给出一个字符串(假设长度最长为1000),求出它的最长回文子串,你可以假定只有一个满足条件的最长回文串.例如,给出字符串 "abcdzdcab",它的最长回文子串为 & ...
- 第5题 查找字符串中的最长回文字符串---Manacher算法
转载:https://www.felix021.com/blog/read.php?2040 首先用一个非常巧妙的方式,将所有可能的奇数/偶数长度的回文子串都转换成了奇数长度:在每个字符的两边都插入一 ...
- [LeetCode] 409. Longest Palindrome 最长回文
Given a string which consists of lowercase or uppercase letters, find the length of the longest pali ...
- leetcode 每日签到 409. 最长回文串
题目: 最长回文串 给定一个包含大写字母和小写字母的字符串,找到通过这些字母构造成的最长的回文串. 在构造过程中,请注意区分大小写.比如 "Aa" 不能当做一个回文字符串. 注意: ...
- Leetcode(5)-最长回文子串(包含动态规划以及Manacher算法)
给定一个字符串 s,找到 s 中最长的回文子串.你可以假设 s 的最大长度为1000. 示例 1: 输入: "babad" 输出: "bab" 注意: &quo ...
随机推荐
- .c和.h档
可一再声明,但不是很多定义 对于一个项目,我们应该要非常好的处理众多的.c和.h文件 1.通过头文件调用库功能:#include <stdio.h> 在非常多场合,源码不便(或 ...
- HDU 3126 Nova [2009 Asia Wuhan Regional Contest Online]
标题效果 有着n巫妖.m精灵.k木.他们都有自己的位置坐标表示.冷却时间,树有覆盖范围. 假设某个巫妖攻击精灵的路线(他俩之间的连线)经过树的覆盖范围,表示精灵被树挡住巫妖攻击不到.求巫妖杀死所有精灵 ...
- 【DP专辑】ACM动态规划总结
转载请注明出处,谢谢. http://blog.csdn.net/cc_again?viewmode=list ---------- Accagain 2014年5月15日 ...
- xsd的解释说明
schema教程 XML Schema是以XML语言为基础的,它用于可替代DTD.一份XML schema文件描写叙述了XML文档的结构XML Schema语言也被称为XML Schema Defin ...
- Android使用HttpURLConnection下载图片
讲到http就必须要了解URI和URL URI (uniform resource identifier)统一资源标志符: URL(uniform resource location )统一资源定位符 ...
- 【LeetCode】3Sum 解决报告
这个问题是我目前的知识回答,不来,只有良好的网上搜索解决方案,发现 K Sum 它是一类问题,但是,互联网是没有更简洁的代码,我想对于谁刚开始学习的人.您可能仍然想看看这个问题该怎么解决,然后看看他们 ...
- 介绍4种HTML5 Canvas库
1.bHive.js Library http://www.bhivecanvas.com/ bHive能创建丰富的用户体验.动画.游戏,能够使开发更简单. demo: http://www.bhiv ...
- 移动端 new CustomEvent('input') 兼容问题
最近在 安卓自带浏览器 上发现 new CustomEvent('input') 不兼容 解决办法 (function () { if(!!window.CustomEvent) return; f ...
- HDU 3366 Passage (概率DP)
Passage Problem Description Bill is a millionaire. But unfortunately he was trapped in a castle. The ...
- hello nodejs
文章1一步:下载.安装文件 打开nodejs官方网站http://www.nodejs.org/download/ .选择须要的版本号.直接打开.默认安装就可以 第二步:编写測试代码: var htt ...