# 【LeetCode Easy】020 Valid Parentheses

Time：2020-1-8

### Easy 020 Valid Parentheses

#### Description:

“()” “[]” {} “three kinds of bracket matching problems to determine whether the parameter string meets the matching requirements
For example: ‘({[]})’ true ‘[{})’ false
Note: empty string is true

#### My Solution:

• The problem of bracket matching is a typical application of stack. When encountering the left bracket, when entering the stack, when encountering the right bracket, see whether the top of the stack is the corresponding left bracket. If not, false
• Time complexity O (n)
• The code is as follows:
• ``````    public boolean isValid(String s) {
if(s.length()==0){
return true;
}
Stack<Character> stack = new Stack<>();
for(char c:s.toCharArray()){
if(c == '(' || c == '{' || c == '['){
stack.push(c);
}else if(stack.size()==0){
return false;
}
else if(c == ')' && stack.peek() == '('){
stack.pop();
}else if(c == ']' && stack.peek() == '['){
stack.pop();
}else if(c == '}' && stack.peek() == '{'){
stack.pop();
}else
return false;
}
if(stack.size() == 0){
return true;
}
return false;
}``````

#### Fast Solution:

1. The idea is the same, but instead of using the stack data structure, a fixed length array is used. For each character of parameter s, the left bracket is put into the array, and the right bracket is used to see whether the last character in the array memory is the corresponding left bracket.

## Comparison and analysis of Py = > redis and python operation redis syntax

preface R: For redis cli P: Redis for Python get ready pip install redis pool = redis.ConnectionPool(host=’39.107.86.223′, port=6379, db=1) redis = redis.Redis(connection_pool=pool) Redis. All commands I have omitted all the following commands. If there are conflicts with Python built-in functions, I will add redis Global command Dbsize (number of returned keys) R: dbsize P: print(redis.dbsize()) […]