LeetCode——Longest Palindromic Substring
Given a string S,
find the longest palindromic substring in S.
You may assume that the maximum length of S is
1000, and there exists one unique longest palindromic substring.
给定一个字符串S,找出当中的最长回文字符子串。
1.枚举全部子串,并推断是否是回文串同一时候记录最大长度。超时。
//找出全部子串,并推断是否是回文串,每次记录长度
public static String longestPalindrome1(String s) {
String ret = "";
int maxlen = 0;
for(int i=0;i<s.length();i++){
for(int j=i+1;j<s.length();j++){
String temp = s.substring(i,j+1);
int len = j - i;
if(isPalindrome(temp)){
if(len > maxlen){
ret = temp;
maxlen = len;
}
}
}
}
return ret;
}
public static boolean isPalindrome(String s){
for(int i=0;i<s.length();i++){
if(s.charAt(i) != s.charAt(s.length() - i - 1))
return false;
}
return true;
}
2.由某个中心向两側扩展寻找,找到能够扩展的最大长度。
public static String longestPalindrome(String s) {
if (s.length() <= 1)
return s;
String longest = s.substring(0, 1);
for (int i = 0; i < s.length(); i++) {
String tmp = helper(s, i, i);
if (tmp.length() > longest.length()) {
longest = tmp;
}
tmp = helper(s, i, i + 1);
if (tmp.length() > longest.length()) {
longest = tmp;
}
}
return longest;
}
public static String helper(String s, int begin, int end) {
while (begin >= 0 && end <= s.length() - 1 && s.charAt(begin) == s.charAt(end)) {
begin--;
end++;
}
return s.substring(begin + 1, end);
}
LeetCode——Longest Palindromic Substring的更多相关文章
- [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 Substring
Given a string S, find the longest palindromic substring in S. You may assume that the maximum lengt ...
- [LeetCode] Longest Palindromic Substring(manacher algorithm)
Given a string S, find the longest palindromic substring in S. You may assume that the maximum lengt ...
- C++ leetcode Longest Palindromic Substring
明天就要上课了,再过几天又要见班主任汇报项目进程了,什么都没做的我竟然有一种迷之淡定,大概是想体验一波熬夜修仙的快乐了.不管怎么说,每天还是要水一篇博文,写一个LeetCode的题才圆满. 题目:Gi ...
- Leetcode: Longest Palindromic Substring && Summary: Palindrome
Given a string s, find the longest palindromic substring in s. You may assume that the maximum lengt ...
- LeetCode:Longest Palindromic Substring 最长回文子串
题目链接 Given a string S, find the longest palindromic substring in S. You may assume that the maximum ...
- Leetcode: Longest Palindromic Substring. java
Given a string S, find the longest palindromic substring in S. You may assume that the maximum lengt ...
- [LeetCode]Longest Palindromic Substring题解(动态规划)
Longest Palindromic Substring: Given a string s, find the longest palindromic substring in s. You ma ...
- Leetcode:Longest Palindromic Substring分析和实现
问题大意是在给定字符串中查找最长的回文子串,所谓的回文就是依据中间位置对称的字符串,比如abba,aba都是回文. 这个问题初一看,非常简单,但是会很快发现那些简单的思路都会带来O(n^3)级别的时间 ...
随机推荐
- hdu 4691 最长的共同前缀 后缀数组 +lcp+rmq
http://acm.hdu.edu.cn/showproblem.php? pid=4691 去年夏天,更多的学校的种族称号.当时,没有后缀数组 今天将是,事实上,自己的后缀阵列组合rmq或到,但是 ...
- 一个非常有用的函数——COALESCE
原文:一个非常有用的函数--COALESCE 很多人知道ISNULL函数,但是很少人知道Coalesce函数,人们会无意中使用到Coalesce函数,并且发现它比ISNULL更加强大,其实到目前为止, ...
- hdu4496 D-City(扭转和支票托收啊 )
主题链接:http://acm.hdu.edu.cn/showproblem.php?pid=4496 D-City Problem Description Luxer is a really bad ...
- 有关Struts2a的ction直接使用response异步问题
假设我们在项目中使用struts2,正在使用ajax而通信时后端程序.为简单起见,我们经常使用下面的方法: ActionContext ac = ActionContext.getCo ...
- java 产生的固体物的基础上 增删改的SQL声明
经过多次修改.最后版本. package com.power.sql; import java.lang.reflect.Field; import java.lang.reflect.Modifie ...
- Duanxx的STM32学习:STM32F103中等容量的功能和外设
版权声明:本文博客原创文章,博客,未经同意,不得转载.
- Tigase XMPP Server在CentOS部署和配置
Tigase XMPP Server在CentOS部署与配置 作者:chszs,转载需注明.博客主页:http://blog.csdn.net/chszs 以下讲述Tigase XMPP Server ...
- Android FM学习中的模块 FM启动过程
最近的研究FM模,FM是一家值我正在学习模块.什么可以从上层中可以看出. 上层是FM按钮的操作和界面显示,因此调用到FM来实现广播收听的功能. 看看Fm启动流程:例如以下图: 先进入FMRadio.j ...
- aul 学习测试(测量)
-------------------aul5 ----------test0------------------------- select file#,rfile#,name from v$dat ...
- Find a way (BFS)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2612 BFS搜索 目标地 并记录下来 之后再判断两段路程之和 代码: #include < ...