본문 바로가기
NOTE/Algorithm

[프로그래머스] 올바른 괄호 C++

by DevAthena 2025. 2. 24.
stack을 사용

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include<string>
#include <iostream>
#include <stack>
 
using namespace std;
 
bool solution(string s)
{
    stack<char> stack;
 
    for (int i = 0; i < s.length(); i++)
    {
        if (s[i] == '(')
        {
            stack.push(s[i]);
        }
        else
        {
            if (stack.empty() == true)
                return false;
 
            stack.pop();
        }
    }
 
    return stack.empty();
}
 
cs

 

counting으로도 풀 수 있는데 문제 의도가 stack이니..