
요것도 레벨2 프로그래머스 문제이다.
이건 이제 괄호를 하나씩 옆으로 옮기면서 괄호가 완전한지 보면 된다.
괄호가 3개여서 스택에 괄호별로 번호를 매겨서 성공했다.
코드가 왜이렇게 길어지는지 모르겠지만... ㅠㅠ
아 0,1,2를 하나씩 if문으로 하지말고 다르게 해봐야겠다.
#include <string>
#include <vector>
#include <stack>
using namespace std;
int solution(string s) {
int answer = 0;
string str = s;
for (int i = 0; i < str.length(); i++) {
stack<int> s;
int pass = true;
for (int j = i; j < i + str.length(); j++) {
if (!pass) break;
int here = j;
if (j >= str.length()) here -= str.length();
//cout << here << " ";
if (str[here] == '(' || str[here] == '{' || str[here] == '[') {
if (str[here] == '(') {
s.push(0);
}
else if (str[here] == '{') {
s.push(1);
}
else {
s.push(2);
}
}
else if (str[here] == ')' || str[here] == '}' || str[here] == ']') {
if (str[here] == ')') {
if (!s.empty()) {
if (s.top() == 0) {
s.pop();
}
else {
pass = false;
break;
}
}
else {
pass = false;
break;
}
}
else if (str[here] == '}') {
if (!s.empty()) {
if (s.top() == 1) {
s.pop();
}
else {
pass = false;
break;
}
}
else {
pass = false;
break;
}
}
else {
if (!s.empty()) {
if (s.top() == 2) {
s.pop();
}
else {
pass = false;
break;
}
}
else {
pass = false;
break;
}
}
}
}
//cout << endl;
if (pass&&s.empty()) {
//cout << i << endl;
answer++;
}
int size_ = s.size();
for (int k = 0; k < size_; k++) {
s.pop();
}
}
return answer;
}'코딩테스트' 카테고리의 다른 글
| 프로그래머스 카카오 프렌즈 컬러링북 (0) | 2021.04.21 |
|---|---|
| 프로그래머스 키패드 누르기 (2) | 2021.04.20 |
| 프로그래머스 신규 아이디 추천 (1) | 2021.04.20 |
| 프로그래머스 크레인 인형 뽑기 (0) | 2021.04.20 |
| 백준 10773 제로 (1) | 2021.04.19 |
댓글