๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Algorithm ๐Ÿง‘๐Ÿป‍๐Ÿ’ป/Leetcode

[Leetcode,c++] Valid Parentheses

by dkswnkk 2021. 11. 9.

๋ฌธ์ œ

 

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;
    }
};

๋Œ“๊ธ€