Why would the following code lead to memory limited exceeded?


  • 0
    G
    class Solution {
        public:
            bool isPalindrome(string s) {
                for(int i = nextIndex(s, -1, 1), j = nextIndex(s, s.size(), -1); i < j; i = nextIndex(s, i, 1), j = nextIndex(s, j, -1))
                {
                    char si = s[i], sj = s[j];
                    if(si >= 'A' && si <= 'Z')
                        si = si - 'A' + 'a';
            
                    if(sj >= 'A' && sj <= 'Z')
                        sj = sj - 'A' + 'a';
            
                    if(si != sj)
                        return false;
                }
                return true;
            }
    
            int nextIndex(string s, int now, int direct)
            {
                for(int i = now + direct; i >= 0 && i < s.size(); i += direct)
                    if(isalpha(s[i]))
                        return i;
                return -1;
        }
    };
    

    Why does this code fragment lead to "MLE" error? Any help? Thanks!


Log in to reply
 

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