0ms C++ solution(only beats 15.xx% ?)


  • 0
    L

    class Solution {
    public:
    bool isValid(string s) {

    	if (0==s.size())
    		return true;
    
    	char	index[256];
    	for (int i=0; i<256; ++i)
    		index[i] = 0;
    
    	index['('] = ')';	index['{'] = '}';	index['['] = ']';
    	index[')'] = '(';	index['}'] = '{';	index[']'] = '[';
    	
    	string	_stack;
    	int		num(s.size());
    	for ( int i(0); i<num; i++ )
    	{
    		if( 0==index[s[i]] )
    			continue;
    
    		if (_stack.size()>0 && index[_stack[_stack.size()-1]]==s[i])
    			_stack.pop_back();
    		else
    			_stack.push_back(s[i]);
    	}
    
    	return _stack.empty();
    }
    

    };


Log in to reply
 

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