【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 ...
随机推荐
- Android RxJava使用介绍(三) RxJava的操作符
上一篇文章已经具体解说了RxJava的创建型操作符.本片文章将继续解说RxJava操作符.包括: Transforming Observables(Observable的转换操作符) Filterin ...
- chrome主页被篡改为360该溶液的导航
昨天,安装游戏后,,发现chrome该主页被篡改为360导航. 进入chrome设置更改主页,再次启动chrome或360导航,后来头发今天chrome快捷方式目标再加上一堆的属性后面360网站导航, ...
- 关于WCF的引用,添加服务和添加web服务的区别
原文:关于WCF的引用,添加服务和添加web服务的区别 本章内容主要是根据我做的实验来阐述这2种添加服务针对WCF的不同之处,我们按照示例一步一步来看. 如下是工程的结构: 该WCF服务是通过控制台程 ...
- DevExpress 12.1 换肤 超级简单的方法(2013-11-5版)
本例子是按照DevExpress 12.1 版本 进行演示.请先准备好DevExpress.BonusSkins.v12.1.dll 和DevExpress.Utils.v12.1.dll 1.首先添 ...
- RH033读书笔记(15)-Lab 16 The Linux Filesystem
Lab 16 The Linux Filesystem Goal: Develop a better understanding of Linux filesystem essentials incl ...
- lua基金会【五岁以下儿童】I/O文件操作
--[[ lua操作相关文件I/O ]]-- --件,假设该文件不存在的话, --lua会帮助我们在你规定的文件夹下创建这个文件,前提是该文件夹要存在 --[[ 同一时候我们应该掌握写入文件的模式: ...
- openSUSE 国内镜像摘要
1. 北交(BJTU): http://mirror.bjtu.edu.cn/opensuse http://mirror.bjtu.edu.cn/packman/suse 2. 华中科大(HUST) ...
- 佛祖保佑,从来没有Bug
<span style="font-size:14px;">// _ooOoo_ // o8888888o // 88" . "88 // (| - ...
- 获取鼠标点击相对于Canva位置的2种方法
如果给Canvas添加 onmousedown事件,获取到的鼠标位置都是相对于当前文档的位置(x,y):
- WPF学习(8)数据绑定
说到数据绑定,其实这并不是一个新的玩意儿.了解asp.net的朋友都知道,在asp.net中已经用到了这个概念,例如Repeater等的数据绑定.那么,在WPF中的数据绑定相比较传统的asp.net中 ...