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

 

프로그래머스

SW개발자를 위한 평가, 교육의 Total Solution을 제공하는 개발자 성장을 위한 베이스캠프

programmers.co.kr

 

내풀이(힌트 참고)

#1 

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

bool solution(string s)
{
    bool answer = true;

    stack<char> st;
    //  (가 없으면 넣고
    // 있으면 현재거랑 비교해서 다르면 pop그대로 가고 그렇지 않으면 push하고
    st.push(s[0]);

    for(int idx = 1; idx < s.size();++idx){
        if(st.empty()){
            st.push(s[idx]);
        }
        else{
            char t = st.top();
            if(s[idx] ==')'){
                if(t == '('){
                    st.pop();
                }
                else{
                    st.push(s[idx]);
                }
            }
            else{
                st.push(s[idx]);
            }
        }
    }
    return answer = st.size() == 0 ? true: false;
}

#2

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

bool solution(string s)
{
    bool answer = true;

    int cnt = 0;
    for(int idx = 0; idx < s.size() ;++idx){
        if(s[idx] == '('){
            cnt++;
        }
        else{
            cnt--;
        }
        if(cnt < 0) return false;
    }
    return answer = cnt == 0 ? true : false;
}

 

 

타인풀이

https://asdsa112.tistory.com/17

 

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

1. 프로그래머스는 제한 시간 이런게 없어서 조건 생각하기가 어렵다고 생각함. 2. 문자열 갯수가 10만 이하라 그냥 순회만 하면서 체크해도 충~~~분할 것 이라 생각했음. 3. 벡터를 추가해서 push_ba

asdsa112.tistory.com

 

https://school.programmers.co.kr/questions/38242

 

 

+ Recent posts