문제 링크입니다: https://programmers.co.kr/learn/courses/30/lessons/12909
코딩테스트 연습 - 올바른 괄호
괄호가 바르게 짝지어졌다는 것은 '(' 문자로 열렸으면 반드시 짝지어서 ')' 문자로 닫혀야 한다는 뜻입니다. 예를 들어 "()()" 또는 "(())()" 는 올바른 괄호입니다. ")()(" 또는 "(()(" 는 올바르지 않은
programmers.co.kr
스택을 이용하여 쉽게 풀 수 있는 문제였습니다.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include<string> | |
#include <iostream> | |
#include <stack> | |
using namespace std; | |
bool solution(string s) | |
{ | |
stack<char> st; | |
for (char c : s) | |
{ | |
if (c == '(') | |
{ | |
st.push(c); | |
} | |
else | |
{ | |
if (st.empty()) | |
{ | |
return false; | |
} | |
st.pop(); | |
} | |
} | |
return st.empty(); | |
} |

개발환경: Programmers IDE
지적, 조언, 질문 환영입니다! 댓글 남겨주세요~
반응형
'알고리즘 > programmers' 카테고리의 다른 글
[Programmers] 다음 큰 숫자 (0) | 2022.03.04 |
---|---|
[Programmers] [3차] n진수 게임 (0) | 2022.03.04 |
[Programmers] [3차] 파일명 정렬 (0) | 2022.02.25 |
[Programmers] [3차] 압축 (0) | 2022.02.25 |
[Programmers] 가장 큰 정사각형 찾기 (0) | 2022.02.23 |