资讯专栏INFORMATION COLUMN

[LintCode/LeetCode] Longest Palindrome Substring

AaronYuan / 2584人阅读

摘要:是左闭右开区间,所以要。,要理解是和之间只有一个元素。循环每次的时候,都要更新子串更大的情况。补一种中点延展的方法循环字符串的每个字符,以该字符为中心,若两边为回文,则向两边继续延展。循环返回长度最长的回文串即可。

Problem

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.

Example

Given the string = "abcdzdcab", return "cdzdc".

Challenge

O(n2) time is acceptable. Can you do it in O(n) time.

Note

动规好题。
.substring(start, end)是左闭右开区间,所以end要+1。
if (s.charAt(i) == s.charAt(j) && (i - j <= 2 || dp[j+1][i-1])),要理解i - j <= 2ij之间只有一个元素。
循环每次i - j > end - start的时候,都要更新子串更大的情况。Time和space都是O(n^2)

补一种中点延展的方法:循环字符串的每个字符,以该字符为中心,若两边为回文,则向两边继续延展。如此,每个字符必对应一个最长回文串。循环返回长度最长的回文串即可。

Solution DP
public class Solution {
    public String longestPalindrome(String s) {
        // Write your code here
        int len = s.length();
        boolean[][] dp = new boolean[len][len];
        int start = 0, end = 0;
        for (int i = 0; i < len; i++) {
            for (int j = 0; j <= i; j++) {
                if (s.charAt(i) == s.charAt(j) && (i - j <= 2 || dp[j+1][i-1])) {
                    dp[j][i] = true;
                    if (end - start < i - j ) {
                        start = j;
                        end = i;
                    }
                }
            }
        }
        return s.substring(start, end+1);
    }
}
方法二:中点延展
public class Solution {
    String longest = "";
    public String longestPalindrome(String s) {
        for (int i = 0; i < s.length(); i++) {
            helper(s, i, 0);
            helper(s, i, 1);
        }
        return longest;
    }
    public void helper(String s, int i, int os) {
        int left = i, right = i + os;
        while (left >= 0 && right < s.length() && s.charAt(left) == s.charAt(right)) {
            left--;
            right++;
        }
        String cur = s.substring(left+1, right);
        if (cur.length() > longest.length()) {
            longest = cur;
        }
    }
}
2018-02-03 Added some comments, same thoughts as solution 2
class Solution {
    public String longestPalindrome(String s) {
        if (s == null || s.length() < 2) return s;
        //return as result
        String longest = s.substring(0, 1);
        for (int i = 0; i < s.length()-1; i++) {
            //get "ABA" type palindrome
            String cur = getPalindrome(s, i, i);
            //get "ABBA" type palindrome, and compare its length with "ABA" type
            if (s.charAt(i+1)==s.charAt(i)) {
                String temp = getPalindrome(s, i, i+1);
                cur = cur.length() > temp.length() ? cur : temp;
            }
            //update longest with cur
            longest = longest.length() > cur.length() ? longest : cur;
        }
        return longest;
    }
    public String getPalindrome(String s, int left, int right) {
        while (left > 0 && right < s.length()-1 && s.charAt(left-1) == s.charAt(right+1)) {
            left--;
            right++;
        }
        //careful with the right bound
        return s.substring(left, right+1);
    }
}

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/65518.html

相关文章

  • 5. Longest Palindromic Substring

    摘要:暴力算法就是找到所有每个都进行的检查。时间复杂度是个调用平均时长为这里唯一确定用的是头尾表示。因为的对称性,我们可以从中间出发向两边延展,找到最长的分为两种基本情况。奇数长度出发点一致,都为偶数长度出发点为相邻的点,和结束是 Given a string s, find the longest palindromic substring in s. You may assume tha...

    APICloud 评论0 收藏0
  • [LintCode/LeetCode] Longest Consecutive Sequence

    摘要:先排序,然后用数组记录每一位上连续序列的长度,每次循环更新最大值存为。 Problem Given an unsorted array of integers, find the length of the longest consecutive elements sequence. Clarification Your algorithm should run in O(n) com...

    buildupchao 评论0 收藏0
  • 查找字符串最长回文

    摘要:查找字符串最长回文思路回文有奇回文和偶回文,是奇回文,是偶回文回文都是中心对称,找到对称点后,同时向前后寻找回文的最长串即可奇回文和偶回文可以归为同一种情况,即以为对称点,以为对称点,但为了代码可读性,可以分开讨论代码奇回文偶回文本题 查找字符串最长回文 Longest Palindromic Substring Given a string s, find the longest ...

    CastlePeaK 评论0 收藏0
  • leetcode部分题目答案之JavaScript版

    摘要:自己没事刷的一些的题目,若有更好的解法,希望能够一起探讨项目地址 自己没事刷的一些LeetCode的题目,若有更好的解法,希望能够一起探讨 Number Problem Solution Difficulty 204 Count Primes JavaScript Easy 202 Happy Number JavaScript Easy 190 Reverse Bi...

    alphahans 评论0 收藏0
  • [LintCode/LeetCode] Restore IP Addresses

    摘要:第一个分割点第二个分割点第三个分割点 Problem Given a string containing only digits, restore it by returning all possible valid IP address combinations. Example Given 25525511135, return [ 255.255.11.135, 255....

    bingo 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<