longestPalindrome

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:

Input: "cbbd"
Output: "bb"

解题思路:最先的是暴力解法,太过于麻烦。这里参考中心扩散解法。对于一个字符串而言,利用两个标记位。从某一个位置的字符开始,以该字符为中心,判断该字符左右两边对称的字符是否相等,如果相等则继续判断,直到标记位移动至字符串首部或者尾部。如果不相等则结束此中心的判断。最后获取到最长的回文之串。

需要注意的是在获取子串时的下标的计算,比较容易出错。

 

class Solution {
    public static int expond(String str,int left,int right)
	{
		int l=left,r=right;
		while(l>=0&&r0)
        {
             int start=0;
     int end=0;
     for(int i=0;iend-start)
    	 {
    		 start=i-(fin-1)/2;
    		 end=i+fin/2;
    	 }
     }
        return s.substring(start,end+1);
}
        else
            return "";
    
    }
}

 

你可能感兴趣的:(【LeetCode】)