[LeetCode][Java]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,找到该字符串中最长的回文子串。假设这个子串的最大长度为1000,并且这个最长回文子串是存在且唯一的。

算法分析:

回文字符串是以中心轴对称的,所以如果我们从下标 i 出发,用2个指针向 i 的两边扩展判断是否相等

注意 回文字符串有奇偶对称之分

代码如下:

    public String longestPalindrome(String s)
    {
		if(s==null||s.length()<=1)
			return s;
		String maxres="";
		String res="";
    	for(int i=0;i<s.length();i++)
		{
			res=getnum(s,i,i);//回文串以i为中心
			if(maxres.length()<res.length())
				maxres=res;

			res=getnum(s,i,i+1);//回文串以i,i+1为中心
			if(maxres.length()<res.length())
				maxres=res;

		}
		return maxres;
    }
	private static String getnum(String s, int begin,int end)
	{
		while(begin>=0&&end<=s.length()-1)
		{
			if(s.charAt(begin)==s.charAt(end))
			{
				begin--;
				end++;
			}
			else
				break;
		}
		return s.substring(begin+1,end);
	}

版权声明:本文为博主原创文章,转载注明出处

时间: 2024-11-07 01:02:07

[LeetCode][Java]Longest Palindromic Substring的相关文章

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

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) 解题思路二: 动态

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 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).首先从字符串的开头位置一直往后遍历,在每次遍历的过程中由该位置向两边扩散,直到找到最长的子回文

【LeetCode】Longest Palindromic Substring 解题报告

DP.KMP什么的都太高大上了,自己想了个朴素的遍历方法. [题目] 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)吧) 从中间向两端搜索,分别找到以每个字母为中心的最长

Leetcode 5 Longest Palindromic Substring (最长回文子字符串)(动态规划)

Leetcode 5 题目描述 Given a string s, find the longest palindromic substring in s. You may assume that the maximum length of s is 1000. 例子 Example 1: Input: "babad" Output: "bab" Note: "aba" is also a valid answer. Example 2: Inp