Leetcode no. 5
2016-04-23 16:49
351 查看
5. 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.
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.
public class Solution { public String longestPalindrome(String str) { int start=0, end=0; for (int i = 0; i < str.length(); i++) { int len1= expand(str,i,i+1); int len2= expand(str,i,i); int len= (len1>len2) ? len1 : len2; if (len>end-start){ start= i-(len-1)/2; end= i+len/2; } } return str.substring(start, end+1); } private int expand(String str, int left, int right){ int l=left, r=right; while ( l>=0 && r<str.length() && str.charAt(l)==str.charAt(r)){ l--; r++; } return r-l-1; } }
相关文章推荐
- java中的compareto方法的详细介绍
- swift 网络请求&json解析
- 【并发编程】Executor架构介绍
- Android实战简易教程-第七十三枪(拨打、接听电话自动免提)
- Java动态绑定机制--笔记
- Android封装支付宝支付
- 机器学习之——多变量线性回归
- span元素的实际应用
- [从头学数学] 第192节 导数及其应用
- # swift2.0 数据结构:元组,枚举,结构体
- 机器学习之——多项式回归和正规方程
- 机器学习之——逻辑回归
- 简易版支付系统部署(单节点)
- cocos2d-x-3.0 的改变,由于变得太多,一点点累积吧!
- 2015國際初中科學奧林匹克 – 香港選拔賽 (比賽結果)
- Log4j
- Codeforces 389A (最大公约数)
- 4.23 GDOI赛前模拟 总结
- LeetCode(69)-Reverse String
- LeetCode(69)-Reverse String