프로그래머스/Lv.2
올바른 괄호 - C++, Stack
아찌방
2025. 2. 22. 20:33
https://school.programmers.co.kr/learn/courses/30/lessons/12909
프로그래머스
SW개발자를 위한 평가, 교육, 채용까지 Total Solution을 제공하는 개발자 성장을 위한 베이스캠프
programmers.co.kr
코드 & 풀이
#include <string>
#include <iostream>
#include <stack>
using namespace std;
bool solution(string s)
{
bool answer = true;
stack<char> c_stack;
for (char c : s) {
if (c == '(') {
c_stack.push(c);
}else {
if (!c_stack.empty()) {
c_stack.pop();
}else {
return false;
}
}
}
return c_stack.empty();
}
스택에는 오로지 '('만 넣는다고 생각했다.
어차피 ')'는 pop() 해야지, 들어가면 결과는 false 가 되기 때문이다.
728x90