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中的最长回文的更多相关文章

  1. LeetCode:Longest Palindromic Substring 最长回文子串

    题目链接 Given a string S, find the longest palindromic substring in S. You may assume that the maximum ...

  2. 转载:LeetCode:5Longest Palindromic Substring 最长回文子串

    本文转自:http://www.cnblogs.com/TenosDoIt/p/3675788.html 题目链接 Given a string S, find the longest palindr ...

  3. [LeetCode] Longest Palindromic Substring 最长回文串

    Given a string S, find the longest palindromic substring in S. You may assume that the maximum lengt ...

  4. [LeetCode] Longest Palindromic Subsequence 最长回文子序列

    Given a string s, find the longest palindromic subsequence's length in s. You may assume that the ma ...

  5. LeetCode之“字符串”:最长回文子串

    题目要求: 给出一个字符串(假设长度最长为1000),求出它的最长回文子串,你可以假定只有一个满足条件的最长回文串.例如,给出字符串 "abcdzdcab",它的最长回文子串为 & ...

  6. 第5题 查找字符串中的最长回文字符串---Manacher算法

    转载:https://www.felix021.com/blog/read.php?2040 首先用一个非常巧妙的方式,将所有可能的奇数/偶数长度的回文子串都转换成了奇数长度:在每个字符的两边都插入一 ...

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

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

  8. leetcode 每日签到 409. 最长回文串

    题目: 最长回文串 给定一个包含大写字母和小写字母的字符串,找到通过这些字母构造成的最长的回文串. 在构造过程中,请注意区分大小写.比如 "Aa" 不能当做一个回文字符串. 注意: ...

  9. Leetcode(5)-最长回文子串(包含动态规划以及Manacher算法)

    给定一个字符串 s,找到 s 中最长的回文子串.你可以假设 s 的最大长度为1000. 示例 1: 输入: "babad" 输出: "bab" 注意: &quo ...

随机推荐

  1. SDL 简单入门学习

    write by 九天雁翎(JTianLing) -- blog.csdn.net/vagrxie 讨论新闻组及文件 概要 实际学习使用SDL创建窗体,并绘制图形. 前言 今天想要做一个简单的demo ...

  2. 代码的未来读书笔记&lt;二&gt;

    代码的未来读书笔记<二> 3.1语言的设计 对Ruby JavaScript Java Go 从服务端client以及静态动态这2个角度进行了对照. 这四种语言因为不同的设计方针,产生了不 ...

  3. 移动web:tab选项卡

    平常做移动端会用到tab选项卡,这和PC端有些区别,移动端是触摸滑动切换,PC端是点击.移入切换. 这里滑动切换就是一个移动端事件的应用,这里主要用到的触摸事件:touchstart.touchmov ...

  4. DICOM医学图像处理:开源库mDCM与DCMTK的比較分析(一),JPEG无损压缩DCM图像

    背景介绍: 近期项目需求,须要使用C#进行最新的UI和相关DICOM3.0医学图像模块的开发.在C++语言下,我使用的是应用最广泛的DCMTK开源库,在本专栏的起初阶段的大多数博文都是对DCMTK开源 ...

  5. 基本介绍LINUX远程PC软件:PUTTY、SecureCRT、X-Manager

    ***********************************************声明************************************************ 原创 ...

  6. JavaFX横幅类游戏开发 教训 游戏贴图

    上一节课,我们即将完成战旗Demo有了一个大概的了解.教训这,我们将学习绘制游戏地图. 由于JavaFX 2.2中添加了Canvas相关的功能,我们就能够使用Canvas来实现游戏绘制了. 游戏地图绘 ...

  7. Unity3D 表对象分类中的实现(C#)

    // Sort by distance in descending order private void SortTargetsByDistance () { targets.Sort(delegat ...

  8. Team Foundation Server 2013 Update 3 下载激活

    http://www.microsoft.com/zh-cn/download/details.aspx?id=43728 支持的操作系统 Windows 7 Service Pack 1, Wind ...

  9. 参加persist.sys物业写权限的方法

    1.于AndroidManifest.xml manifest添加属性android:sharedUserId="android.uid.system" 2.假设AndroidMa ...

  10. 【SQL Server性能优化】SQL Server 2008该表压缩

    当数据库是比较大的,而当你想备份,我们可以启动数据库备份压缩.这项由于备份文件比较小的压缩,所以整个备份的更快的速度,同时还低了磁盘空间的消耗. 当然还有一方面.肯定会添加cpu的消耗.只是一般的se ...