Leetcode Longest palindrome substring
2015-06-17 09:08
357 查看
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.
一个palindrome可以用
if odd, mid加长度表示
if even, mid, mid+1 加长度
public class Solution { public String longestPalindrome(String s) { if(s.length()<=1)return s; String r=""; int max=1; for(int i=0;i<s.length()-1;i++) { int t=palindrome(s,i,i); if(t>max) { max=t; int offset=(t-1)/2; r=s.substring(i-offset, i+offset+1); } t=palindrome(s,i,i+1); if(t>max) { max=t; int offset=(t-2)/2; r=s.substring(i-offset,i+offset+2); } } return r; } int palindrome(String s, int p1, int p2) { while(p1>=0 && p2<s.length() && s.charAt(p1)==s.charAt(p2)) { p1--; p2++; } return p2-p1-1; } }
相关文章推荐
- 【LeetCode】 Longest Palindrome Substring
- LeetCode-5_Longest Palindrome Substring
- 【Leetcode】Longest Palindrome Substring
- LeetCode the longest palindrome substring
- [LeetCode] Longest Palindrome Substring 详细分析
- [LeetCode] Longest Palindrome Substring 具体分析
- 【Leetcode】Longest palindrome substring
- leetcode 3 Longest Substring Without Repeating Characters最长无重复子串
- [LeetCode] Longest Substring with At Most Two Distinct Characters
- [LeetCode]Longest Substring Without Repeating Characters
- [Leetcode] Longest Palindromic Substring [Unsolved]
- Longest Palindromic Substring leetcode java
- 【LeetCode】LeetCode——第3题: Longest Substring Without Repeating Characters
- leetcode——3—— Longest Substring Without Repeating Characters
- leetcode5-Longest Palindromic Substring(最长回文子串)
- 【Leetcode】【Medium】Longest Substring Without Repeating Characters
- LeetCode 5 Longest Palindromic Substring
- leetcode之Longest Palindromic Substring
- 【leetcode】Longest Substring Without Repeating Characters
- LeetCode3:Longest Substring Without Repeating Characters