my Very Very simple c++ code


  • 0
    N
    class Solution {
        public:  
            string longestPalindrome(string s) {
                string ans = s.substr(0, 1); 
                // 以mid为中心,两边查找
                for(double mid=0.5; mid<s.length(); mid += 0.5) {
                    double d = (mid - int(mid) == 0 ?1:0.5);
                    while(mid - d >= 0 && mid + d < s.length()) {
                        if(s[mid - d] != s[mid + d]) break;
                        ++d;
                    }   
                    if(mid - d + 1 >= 0) {
                        string sub = s.substr(mid - d + 1, 2*d-1);
                        if(ans.length() < sub.length()) ans = sub;
                    }   
                }
                return ans;                                                                                         
            }       
    };
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.