【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 ...
随机推荐
- 【原创】POJ 1703 && RQNOJ 能量项链解题报告
唉 不想说什么了 poj 1703,从看完题到写完第一个版本的代码,只有15分钟 然后一直从晚上八点WA到第二天早上 最后终于发现了BUG,题目要求的“Not sure yet.”,我打成了“No s ...
- cocos2dx 3.2 定义自己使用rapidjson阅读json数据
一.说明 我在这里得到的只是一个简单的定义string和Int种类,其他数据类型可以被替换向上. 两.头文件 class JsonReadUtils { public: static JsonRead ...
- hdu 4944 FSF’s game(数论)
题目链接:hdu 4944 FSF's game 题目大意:给定N,能够用不大于N的长a和宽b.组成N∗(N−1)2种不同的矩形,对于每一个矩形a∗b要计算它的值,K为矩形a,b能够拆分成若干个K∗K ...
- javascript有用小技巧—实现分栏显示
记得给师哥师姐測试考试系统的时候,看到他们的考试页面能够实现隐藏左边的考生信息部分,当时认为好高大上.好人性化. 如今学了javascript,我也能实现这个功能了,以下来显摆一下. 1.页面设计: ...
- Hosting WCF Service
之前在博客几个实例DemoWCF服务寄宿到控制到应用程序中,这篇来总结一下,经常使用的几种宿主的方式. 1.Self-Hosting 一个WCF服务可以寄宿在控制台应用程序或者WinForms app ...
- Dubbo与Zookeeper、SpringMVC整合和使用(负载均衡、容错)(转)
互联网的发展,网站应用的规模不断扩大,常规的垂直应用架构已无法应对,分布式服务架构以及流动计算架构势在必行,Dubbo是一个分布式服务框架,在这种情况下诞生的.现在核心业务抽取出来,作为独立的服务,使 ...
- chrome(转)
阅读目录 Chrome的隐身模式 Chrome下各种组合键 Chrome的about指令 chrome://accessibility 查看浏览器当前访问的标签 chrome://appcac ...
- 香蕉派路由功Openwrt、Android功耗对照測试
路由这个东西是要长期通电使用的,所以功耗也是须要关注的.如今香蕉派路由已经有了openwrt和android两个 系统,这两个系统的功耗是否一样呢? 測试工具:QUIGG的德国产功耗測试仪一个.手机充 ...
- 从XML文件乱码问题,探寻其背后的原理(转)
由于网友反应本文图片不能显示,由于时间关系未能及时修正.请访问原文地址: 本文出自http://blog.csdn.net/dinglang_2009/article/details/6895355, ...
- 给Activity设置Dialog属性,点击区域外消失;
1.在AndroidManifest.xml中给Activity设置样式: <activity android:name=".MyActivity" ...