Share my concise C++ code -- with 4ms time


  • 0
    G
    class Solution {
    public:
        bool isValid(string s) {
            if (s.length() == 0)
                return true;
            else{
                string::size_type position = s.find("()");
                if (position == s.npos)
                    position = s.find("[]");
                if (position == s.npos)
                    position = s.find("{}");
                if (position != s.npos)
                    return isValid(s.erase(position, 2));
            }
            return false;
    
    
            
        }
    };

  • 0
    H

    Awesome, but I think when the string grow long, the cost of erase function will increase very quick.


Log in to reply
 

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