Java for LeetCode 005 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.

解题思路一:

暴力枚举

共N^2量级个子串(从下标零开始),每次检查需一个for循环,等于是3重for循环,时间复杂度O(n^3)

解题思路二:

动态规划

设定一个表格table[][],其中table[i][j]表示substring(i,j+1)是不是Palindromic,时间复杂度为O(n^2)空间复杂度也为O(n^2)。

Java代码如下:

	static public String longestPalindrome(String s) {
		if(s.length()==1)return s;
    	int[][] table=new int[s.length()][s.length()];
    	int beginIndex = 0,endIndex = 0;
    	//初始化第一、第二条斜线
    	for(int i=0;i<s.length();i++){
    		table[i][i]=1;
    		if(i==s.length()-1)break;
    		if(s.charAt(i)==s.charAt(i+1)){
    			table[i][i+1]=1;
    			beginIndex=i;
    			endIndex=i+1;
    		}
    	}
    	//给第k条斜线赋值
    	for(int k=2;k<s.length();k++){
    		for(int i=0;i<s.length()-k;i++){
    			if(table[i+1][i+k-1]==1&&s.charAt(i)==s.charAt(i+k)){
        			table[i][i+k]=1;
        			beginIndex=i;
        			endIndex=i+k;
    			}
    		}
    	}
    	printTable(table);
    	return s.substring(beginIndex,endIndex+1);
    }

	public static void printTable(int table[][]){
    	for(int i=0;i<table.length;i++){
    		for(int j=0;j<table[i].length;j++){
    			System.out.print(table[i][j]+" ");
    		}
    		System.out.println("");
    	}
	}

提交结果,TimeExceed。证明时间复杂度为O(n^2)是不能提交通过的。

解题思路三:

中心法,对S中每一个字符及重复的双字符为中心,进行遍历。时间复杂度为O(n^2),在leetcode上竟然Accepted!

Java代码如下:

static public 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);
	}

解题思路四:

Manacher’s algorithm

时间: 2024-12-09 19:41:06

Java for LeetCode 005 Longest Palindromic Substring的相关文章

LeetCode 005 Longest Palindromic Substring - Java

Given a string s, find the longest palindromic substring in s. You may assume that the maximum length of s is 1000. Example: Input: "babad" Output: "bab" Note: "aba" is also a valid answer. Example: Input: "cbbd" Ou

[LeetCode] 005. Longest Palindromic Substring (Medium) (C++/Java/Python)

索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 005.Longest_Palindromic_Substring (Medium) 链接: 题目:https://oj.leetcode.com/problems/Longest-Palindromic-Substring/ 代码(github):https://github.com/illuz/leetcode

【LeetCode】005 Longest Palindromic Substring

题目:LeetCode 005 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,找到S中的最长回文子串.假定S最长为100

LeetCode #5 Longest Palindromic Substring (M)

[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. [Analysis] 这题的思路有很多种,网上也有各种讨论.这里我采用的是个人觉得比较好理解的一种利用Dynamic Progra

LeetCode(4) || Longest Palindromic Substring 与 Manacher 线性算法

LeetCode(4) || Longest Palindromic Substring 与 Manacher 线性算法 题记 本文是LeetCode题库的第五题,没想到做这些题的速度会这么慢,工作之余全部耗在这上面了,只怪自己基础差.本文主要介绍使用Manacher线性算法来求解字符串的最长回文子字符串. 题目 Given a string S, find the longest palindromic substring in S. You may assume that the maxim

LeetCode:Longest Palindromic Substring

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. 动态规划解法 T(n) = O(n^2)  ,S(n) = O(n^2); Solutio

LeetCode-Algorithms #005 Longest Palindromic Substring, Database #179 Consecutive Numbers

LeetCode-Algorithms #005 Longest Palindromic Substring 英语学习时间palindromic: [医] 复发的, 再发的 在数学和计算机上,就指回文 这道题目就是找出给定字符串中最长的回文子串, 可以假定原字符串的长度不超过1000 直接遍历来做肯定是不难, 但也可以想见一定是慢得可以. 那么我的另一个想法是, 从原串中的每一个字符, 或每两个字符中间的空隙开始, 向左右两边判断是否为回文串, 最后找出最长的 1 class Solution

LeetCode 5 Longest Palindromic Substring(C,C++,Python,Java)

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. Solution: 以每一个字符为中间查找回文串,然后记录下最大的回文串,时间复杂度O(n^2) 题目大意: 给定一个字符串,求字符

Java [leetcode 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. 解题思路: 遍历方法,时间复杂度为O(n).首先从字符串的开头位置一直往后遍历,在每次遍历的过程中由该位置向两边扩散,直到找到最长的子回文