Recent Posts
«   2025/03   »
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 29
30 31
Today
Total
관리 메뉴

Try

[Programmers/C++] 올바른 괄호 본문

Algorithm/Programmers

[Programmers/C++] 올바른 괄호

HAS3ONG 2019. 4. 7. 19:21

출처

https://programmers.co.kr/learn/courses/30/lessons/12909

 

알고리즘 연습 - 올바른 괄호 | 프로그래머스

실행 결과가 여기에 표시됩니다.

programmers.co.kr

소스

#include <string>
#include <iostream>
#include <stack>

using namespace std;

bool solution(string s)
{
    bool answer = true;
    stack<char> t;
    
    for(int i = 0; i < s.size(); i++){
        if(s[i] == '('){
            t.push(s[i]);
        }
        
        else if(s[i] == ')'){
            if(t.empty()){
                return false;
            }
            
            if(t.top() == '('){
                t.pop();
            }
            else{
                return false;
            }
        }
        else {
            return false;
        }
        
    }
    if(!t.empty()){
        return false;
    }
    return answer;
}
Comments