Longest Palindromic Substring
2015-07-15 21:10
316 查看
题目: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.
也就是求最长回文子字符串。回文就是指从左到右和从右到左是相同的字符串。如abba,aba。
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.
也就是求最长回文子字符串。回文就是指从左到右和从右到左是相同的字符串。如abba,aba。
public static String longestPalindrome(String s) { if(s.isEmpty()) return null; if(s.length()==1) return s; String longest=s.substring(0,1); for(int i=0;i<s.length();i++){ // get longest palindrome with center of i String temp=helper(s, i, i); if(temp.length()>longest.length()) longest=temp; // get longest palindrome with center of i,i+1 temp=helper(s, i, i+1); if(temp.length()>longest.length()) longest=temp; } return longest; } // Given a center, either one letter or two letter, Find longest palindrome public static String helper(String s,int start,int end){ while(start>=0&&end<s.length()&&s.charAt(start)==s.charAt(end)){ start--; end++; } return s.substring(start+1,end); }
相关文章推荐
- Java静态字段(属性、方法、类别)
- StringBuffer,StringBuilder(java基础毕老师的源码)
- 使用springMVC的详细步骤 SpringMVC与Struts2对比
- 1220 数字三角形(4解)
- gethostbyaddr 和 gethostbyname 给定主机的信息
- 读《《图解TCP-IP》》有感
- mousewheel 与 DOMMouseScroll
- bzoj1178
- 08 循环
- 替换空格(面试题4)
- 怎么取得已知时间的下一天
- hdu 1560 DNA sequence(迭代加深搜索)(经典题)
- 4.4百分比面积图
- 人丑就要多读书(10)
- bzoj1151
- Swift面向对象基础(上)——Swift中的枚举
- 海拉但是
- @class与#import
- ubuntu shutdown command
- Java值传递以及引用的传递、数组的传递!!