To determine the legality of the brackets


START ⼀ input strings, which comprises {} Six kinds of brackets, parentheses you judge this string of legality.

Input: "()[]{}"
Output: true
Input: "([)]"
Output: false
Input: "{[]}"
Output: true

Only one brackets

Only a string of parentheses, brackets is legal if you want, you must:
每个右括号 ) 的左边必须有⼀个左括号 ( 和它匹配.
⽐ as saying string ())) ((middle, left and right two brackets in the middle of no left parenthesis matching, so this combination is not legitimate brackets
以0为界,是正是负来判断

bool isValid(string str){
	//待匹配的左括号数量
	int left = 0;
	for(char c : str){
		if(c == '(' )
			left ++;
		else //遇到右括号
			left --;
		
		if(left < 0)
			return false;
	}
	return left == 0;
}

Multiple brackets

If a plurality of variables to determine whether each of [{(unlikely lines, such as [ ( ] )illegal, that is 位置不能交叉, once the right is a sign that the stack closest to look for, is on the left (before the match has pop-up, does not affect)
the use of advanced after the deal brackets problem is useful
to use a stack called left, met on the stack left parenthesis, right parenthesis encounter went to find the nearest stack left parenthesis, to see if they match (in parentheses are treated equally, only about points )

class Solution {
    public boolean isValid(String s) {
        if(s == null || s.length() <= 0) return true;
        // stack<char> left;
        Stack<Character> left =new Stack<Character>();
        for(char c : s.toCharArray()){
            if(c =='(' || c=='{' || c=='[')
                left.push(c);
            else//字符c是右括号
                if(!left.empty() && leftOf(c) == left.peek())
                    left.pop();
                else
                    //和最近的左括号不匹配
                    return false;
        }
        //是否所有的左括号都被匹配了
        return left.empty();
    }
    char leftOf(char c){
        if( c == '}') return '{';
        if( c == ')') return '(';
        else return '[';
        // else return '';
    }
}

Note

Brackets problem is a classic stack structure enables problem
python does not stack structure, need to use simulation List
python purposes for loop through a list of strings tuples dictionary: for value in my_str:

Stack java in only a non-argument constructor:
Push (NUM) // stack
pop () // top element from the stack
empty () // determines whether the stack is empty
peek () // Get the top element
search (num ) // distance from the top of the stack to the position of the element for the first time

java string manipulation

java Traversal string
for loop: s.charAt (I) == 'A')
char [] s.toCharArray C = (); for (C char. 1: C)

java is determined whether an empty string
if(s == null || s.length() <= 0); // comparison string length
s == null presence is necessary: If a String is null, away for the equals (String) or length () throws operations such java.lang.NullPointerException . s == null and order must appear before, or the same throws java.lang.NullPointerException.

Published 149 original articles · won praise 5 · views 30000 +

Guess you like

Origin blog.csdn.net/qq_26327971/article/details/105081872