프로그래머스 - 짝지어 제거하기 - Level 2

Byungwoong An·2021년 7월 1일
0

문제

풀이전략

  1. 스택을 사용해서 풀어야하는 문제이다. 스택의 top과 순차적으로 제거해주며 풀어주면 된다.

코드

#include <iostream>
#include<string>
#include<stack>
using namespace std;

int solution(string s)
{
    int answer = -1;

    
    stack<char> st;
    for(int i=0; i<s.length(); i++){
        
        if(st.empty()) st.push(s[i]);
        else{
            char ret = st.top();
            if(ret == s[i]){
                st.pop();
            }
            else st.push(s[i]);
        }  
    }
    if(st.empty()) answer = 1;
    else answer = 0;
    
    return answer;
}

소감

전형적인 스택문제이다. 어떻게 푸는지만 알아두자.

profile
No Pain No Gain

0개의 댓글