Valid Parentheses)
Question: from today on, a question on leetcode is published every day. leetcodeleetcode is an OJ website outside China. Up to now, there have been a total of 158 questions, these questions contain a lot of knowledge about data structures. They can not only improve their coding capabilities, but also learn a lot about data structures. All these 158 questions can be found online, and many people have completed these questions. Why do I have to write a blog every day? My idea is very simple. The answer to others' questions is others' knowledge. I have to get a glimpse of it on paper. I know that I have to do this. I can only know how many pounds I have. Another reason is to record the refreshed questions in a blog. It is also a note. PS: welcome like-minded friends to give questions together, learn together, and make progress together. My programming language: Java topic description Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid. the brackets must close in the correct order, "()" and "() [] {}" are all valid but "(]" and "([)]" are not. total Accepted: 25934 Total Submissions: 92373 class: Easy analysis this question allows us to complete the matching function of brackets. This function is frequently used in compilation principles and other courses. To implement this function, it is simple and difficult. The key is to check whether the data structure is used. What structure should I use? By the way, we often use the stack structure. In fact, in the existing syntax analyzer, this function is implemented using stacks. Pseudo-code input: a bracket string, which only contains the brackets. Input: whether the input string is valid. Valid output is true. Invalid output is false. The character pointer I = 0 extracts the current character of the character string according to the character pointer. If the current character is empty, jump to 5 if the current character is left parenthesis ), press directly into the stack. If the current character is parentheses ()]}, an element is popped up from the stack. If the pop-up element matches the current character, I ++ returns to 2; otherwise, false is returned. If the stack is empty, true is returned. Otherwise, the complexity analysis of false is easy to see that each character of the string in parentheses only needs to be considered once. The time complexity is O (n) source code import java. util. stack; public class ValidParentheses {public boolean isValid (String s) {// 1 stands for (2 stands for [3 stands for {4 stands) 5 stands for] 6 stands for} // The first boundary condition if (s. length () = 0) return true; Stack <Integer> stack = new Stack <Integ Er> (); char c; int temp; for (int I = 0; I <s. length (); I ++) {c = s. charAt (I); if (c = '(') stack. add (1); else if (c = '[') stack. add (2); else if (c = '{') stack. add (3); else {// second boundary condition if (stack. empty () return false; if (c = ') temp = 4; else if (c ='] ') temp = 5; else temp = 6; if (stack. peek ()! = Temp-3) return false; else stack. pop () ;}} if (stack. isEmpty () return true; else return false;} public static void main (String [] args) {ValidParentheses vp = new ValidParentheses (); String test = "] ["; system. out. print (vp. isValid (test) ;}} how others do it if s = "": return True if len (s) % 2 = 1: return False else: while ("()" in s or "[]" in s or "{}" in s): s = s. replace ("()", "") s = s. replace ("[]", "") s = s. replace ("{}", "") return self. the code above isValid (s) return False is written in Python. Although the recursion efficiency is not high, it is concise and understandable.