https://oj.leetcode.com/problems/longest-palindromic-substring/
Given a string S, find the longest palindromic substring in s. The maximum length of S is assume, and there exists one unique longest palindromic substring.
A relatively simple solution, traversing the array from left to right, with the current number as the axis, or two of the same number as the axis, (two cases), spread to both sides. The terminating condition is that it spreads to the beginning or end of the array, or the two segments of the element are inconsistent. Records the length of each case, and finally returns the longest start and end coordinates. The solution is mainly to pay attention to two kinds of situations. The time complexity is O (n^2).
Public classSolution { Publicstring Longestpalindrome (string s) {intStart = 0; intEnd = 0; intMaxstart = 0; intMaxend = 0; intMaxLength = 0; //with a number as the axis of symmetry for(inti = 0; I < s.length (); i++) {Start=i; End=i; while(Start >= 0 && End < S.length () && s.charat (start) = =S.charat (end)) {Start--; End++; } Start++; End--; intLength = End-start + 1; if(Length >maxLength) {MaxLength=length; Maxstart=start; Maxend=end; } } //with some two symmetrical if(S.length () > 1){ for(inti = 1; I < s.length (); i++){ if(S.charat (i) = = S.charat (i-1) ) {Start= I-1; End=i; while(Start >= 0 && End < S.length () && s.charat (start) = =S.charat (end)) {Start--; End++; }} Start++; End--; intLength = End-start + 1; if(Length >maxLength) {MaxLength=length; Maxstart=start; Maxend=end; } } } returnS.substring (Maxstart, Maxend + 1); }}
Longest palindromic Substring