【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 ...
随机推荐
- http与https与socket tcp/IP与UDP 协议等
网络由下往上分为: 物理层-- 数据链路层-- 网络层-- IP协议 传输层-- ...
- effective c++ 条款13 use object to manage resources.
请求的系统资源需要最终还回系统,为了避免遗忘返还这个动作,可以利用析构函数在object销毁时自动调用的特点来实现. 简单说就是用object来管理资源. 以内存资源为例 class Investme ...
- 自定义ViewGroup实现垂直滚动
转载请表明出处:http://write.blog.csdn.net/postedit/23692439 一般进入APP都有欢迎界面,基本都是水平滚动的,今天和大家分享一个垂直滚动的例子. 先来看看效 ...
- 从一开始,说出事java匿名内部类
java内部类.匿名类原本以为它们的使用已经很滑, 成绩, 就在昨天晚上12指向时钟发生重大事故.事故的严重程度再说吧,那是因为我没有睡一晚睡眠. 那以下先用一段模拟代码来描写叙述下我出现的问题的: ...
- c++爱问的面试问题
1.static_cast,dynamic_cast,reinterpret_cast,const_cast四种转换. 2.const行为 3.malloc/free, new/delete差额 4. ...
- POJ 1384 Piggy-Bank 背包DP
所谓的全然背包,就是说物品没有限制数量的. 怎么起个这么intimidating(吓人)的名字? 事实上和一般01背包没多少差别,只是数量能够无穷大,那么就能够利用一个物品累加到总容量结尾就能够了. ...
- CentOS在安装配置 Ngnix_tomcat_PHP_Mysql
安装Nginx yum install nginx 假设显示找不到 nginx包,新建一个文件/etc/yum.repos.d/nginx.repo,内容: [nginx] name=nginx re ...
- activity-alias使用
activity-alias这是android为了重新使用Activity设计. 当Activity的onCreate()在方法,运行getIntent().getComponent().getCla ...
- poj2112 Optimal Milking --- 最大流量,二分法
nx一个挤奶器,ny奶牛,每个挤奶罐为最m奶牛使用. 现在给nx+ny在矩阵之间的距离.要求使所有奶牛挤奶到挤奶正在旅程,最小的个体奶牛步行距离的最大值. 始感觉这个类似二分图匹配,不同之处在于挤奶器 ...
- 读书时间《JavaScript高级程序设计》二:面向对象
接着上次的进度,开始看第6章. 第6章 面向对象的程序设计 理解对象 创建自定义对象最简单的方式就是创建一个 Object 的实例,然后为它添加属性和方法. var person = new Obje ...