Algorithm 🧑🏻‍💻/Leetcode

[Leetcode,c++] Valid Parentheses

dkswnkk 2021. 11. 9. 21:53

문제

 

Valid Parentheses - LeetCode

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

leetcode.com

 

코드

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

        stack<char>st;

        for(int i=0; i<s.length(); i++){
            char inp = s[i];
            if(st.empty()) st.push(inp);
            else{
                char top = st.top();
                if(top=='('&&inp==')') st.pop();
                else if(top=='{'&&inp=='}') st.pop();
                else if(top=='['&&inp==']') st.pop();
                else st.push(inp);
            }
        }
        if(st.empty()) return true;
        else return false;
    }
};