9/2

우선 생각나는대로 풀어보기
#include <iostream>
#include <string>
using namespace std;
int solution(string s){
int answer = -1;
for(int i =s.size()-1; i>0; --i){
if(s[i] == s[i-1]) {
s.erase(i-1, 2);
i++;
}
//cout << s << " ** ";
}
return s.size()==0? 1 : 0;
}
이렇게 푸니까 확실히 시간이 너무 오래걸렸다
#include <iostream>
#include <string>
using namespace std;
int solution(string s){
int n = s.size()-2;
while(n >= 0){
if(s[n] == s[n+1]) s.erase(n,2);
n--;
//cout << s << " ** ";
}
return s.size()==0? 1 : 0;
}
보완한다고 했는데 이것도 마찬가지...
그럼 이럴때 써야되는게 뭐다?!?!?
STACK!!!!!
#include <iostream>
#include <string>
#include <stack>
using namespace std;
int solution(string s){
int n = s.size()-2;
stack <char> st;
st.push(s[0]);
for(int i = 1; i<s.size(); i++){
if(!st.empty() && st.top() == s[i]) st.pop();
else st.push(s[i]);
}
return st.size()==0? 1 : 0;
}'C++ 문제풀기 > 백준-프로그래머스' 카테고리의 다른 글
| 프로그래머스 | 2개 이하로 다른 비트 (0) | 2025.09.13 |
|---|---|
| 프로그래머스 | 택배상자 (0) | 2025.09.13 |
| 프로그래머스 | N 보다 큰 수 (0) | 2025.09.13 |
| 프로그래머스 | 이진변환 (0) | 2025.09.13 |
| 프로그래머스 | 최댓값과 최솟값 (0) | 2025.09.13 |