[Leetcode] 020. Valid parentheses (Easy) (C++/java/python)

Source: Internet
Author: User

Index: [Leetcode] leetcode key index (C++/JAVA/PYTHON/SQL)
Github:https://github.com/illuz/leetcode

020.valid_parentheses (Easy) links

Title: https://oj.leetcode.com/problems/valid-parentheses/
Code (GitHub): Https://github.com/illuz/leetcode

Test Instructions

Determines whether a parenthesis string is valid.

Analysis

Directly with the stack simulation, very simple.
The parentheses matching in Java can be written with if, can also be used, and can be used HashMap<Character, Character> "(){}[]".indexOf(s.substring(i, i + 1) . (This discussion can also be used for C + + and Python)

Here C + + is used if match, Java with IndexOf, Python with Dict.

Code

C++:

Class Solution {Public:bool IsValid (string s) {stack<char> Stk;int len = s.length (); for (int i = 0; i < len; i++)  {if (s[i] = = ' (' | | s[i] = = ' [' | | s[i] = = ' {') {Stk.push (s[i]);} else {if (Stk.empty ()) return false;if (' stk.top () = = ' (' && S[i] = = ') Stk.pop (); else if (stk.top () = = ' [' && s[i] = = '] ') stk.pop (); else if (stk.top () = = ' {' &am p;& S[i] = = '} ') Stk.pop (); Elsereturn false;}} return Stk.empty ();}};


Java:

public class Solution {public    Boolean isValid (String s) {        stack<integer> Stk = new stack<integer> (); for        (int i = 0; i < s.length (); ++i) {            int pos = "() {}[]". IndexOf (S.substring (i, i + 1));            if (pos% 2 = = 1) {                if (stk.isempty () | | Stk.pop ()! = pos-1)                    return false;            } else {                stk.push (POS);            }        }        return Stk.isempty ();}    }


Python:

Class solution:    # @return A Boolean    def isValid (self, s):        MP = {') ': ' (', '] ': ' [', '} ': ' {'}        STK = []
   
    for ch in s:            if ch in ' ([{':                stk.append (CH)            else:                if not STK or mp[ch]! = Stk.pop ():                    return FALSE
    return not STK
   



[Leetcode] 020. Valid parentheses (Easy) (C++/java/python)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.