Created
September 14, 2018 23:54
-
-
Save zhangxiaomu01/7537e95c89db86281b221b6271f00af8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { | |
public: | |
bool isPalindrome(string &s) | |
{ | |
int len = s.size(); | |
for(int i = 0; i< len/2; i++) | |
{ | |
if(s[i] != s[len - i - 1]) | |
return false; | |
} | |
return true; | |
} | |
string longestPalindrome(string s) { | |
int len = s.size(); | |
int iStart = 0, Max = 0; | |
string temp; | |
for(int i =0; i< len; i++) | |
{ | |
for (int j = i; j < len ; j++) | |
{ | |
temp = s.substr(i, j-i+1); | |
if(isPalindrome(temp) && j-i + 1 > Max) | |
{ | |
Max = j-i+1; | |
iStart = i; | |
} | |
} | |
} | |
return s.substr(iStart, Max); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment