longest Palindrome substring

class Solution {
public:
    string longestPalindrome(string s) {
        // Start typing your C/C++ solution below
        // DO NOT write int main() function
        int len = s.size();
        int temp = 1;
        int max = 0;
        int index;
        for (int i =1 ; i< len; i++){
            int left = i-1; 
            int right = i+1;
            while (left >= 0 && right < len){
                if (s[left]==s[right]){
                    left--;
                    right++;
                }
                else
                    break;
            }
            temp = right-left-1;
            if (temp>max){
                max = temp;
                index = left+1;
            }
            
            left = i-1;
            right = i;
            while (left >= 0 && right < len){
                if (s[left]==s[right]){
                    left--;
                    right++;
                }
                else
                    break;
            }
            temp = right-left-1;
            if (temp>max){
                max = temp;
                index = left+1;
            }
        }
        return s.substr(index, max); 
    }
};

你可能感兴趣的:(String)