문제

풀이

class Solution {
public:
    bool isValid(string s) {
        stack<char> st;

        for (char ch : s)
        {
            if (isOpening(ch))
            {
                st.push(ch);
                continue;
            }

            if (st.empty())
            {
                return false;
            }

            char t = st.top();
            st.pop();

            if ((ch == ')' && t == '(')
            || (ch == ']' && t == '[')
            || (ch == '}' && t == '{'))
            {
                continue;
            }

            return false;
        }

        return st.empty();
    }

private:
    bool isOpening(char c)
    {
        switch(c)
        {
            case '(':
            case '[':
            case '{':
                return true;
        }

        return false;
    }
};

음, 이건 뭐 스택을 처음 배울 때 푸는 문제 같은 거니까 설명은 생략!

이 포스트는 달레 스터디(6주차)에서 진행한 Blind 75 문제집 풀이의 일부입니다.

댓글남기기