일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
Tags
- AOP
- 백준
- mock
- Docker
- MSA
- 우테코
- AWS
- JUnit5
- 의존성
- 우아한테크코스
- Level2
- 트랜잭션
- REDIS
- 코드리뷰
- 우아한세미나
- JPA
- 자바
- HTTP
- 레벨2
- 프로그래머스
- Spring Batch
- 서블릿
- 스프링 부트
- yml
- 스프링부트
- 세션
- 프리코스
- Paging
- CircuitBreaker
- 미션
Archives
- Today
- Total
늘
Level.2_괄호 변환 본문
728x90
카카오 level2! 처음에 함수로 안만들고 구현했더니 틀렸었다. 아무래도 재귀함수를 이용해야 해서 그랬던것 같다.
그래도 알고리즘 설명이 문제에 나와있어서 손쉽게 풀수 있었다!
#include <string>
#include <vector>
using namespace std;
//u가 올바른 문자열인지
bool All(string u){
vector<char> vec;
vec.push_back('a');
for(int i=0;i<u.length(); ++i){
if(u[i] == '('){
vec.push_back(u[i]);
}
else if(u[i] == ')'){
if(vec.back() == '('){
vec.pop_back();
continue;
}
else{
vec.push_back(u[i]);
}
}
}
if(vec.size()==1){
return true;
}
return false;
}
string solution(string p) {
string answer = "";
string u="";
int l=0;
int r=0;
string v="";
vector<char> vec;
if(p.length()==0){
return answer;
}
for(int i=0;i<p.length(); ++i){
if(p[i] == '('){
u+=p[i];
l++;
}
if(p[i]==')'){
u+=p[i];
r++;
}
if(l!=0 && r!=0 &&l == r){
v = p.substr(i+1);
break;
}
}
if(All(u)){
u += solution(v);
return u;
}
else{
answer+='(';
answer+=solution(v);
answer+=')';
for(int i=1;i<u.length()-1; ++i){
if(u[i]==')'){
answer+='(';
}
else{
answer+=')';
}
}
return answer;
}
}
728x90
'알고리즘_프로그래머스 > KAKAO' 카테고리의 다른 글
[프로그래머스] C/C++ [2020 KAKAO BLIND RECRUITMENT] 문자열 압축 (0) | 2021.10.26 |
---|---|
c/c++ 2018 카카오 블라인드 [3차] n진수 게임 (0) | 2021.08.24 |
C/C++ 2018 KAKAO BLIND RECRUITMENT [3차] 파일명 정렬 (0) | 2021.08.06 |
2018 KAKAO BLIND RECRUITMENT [추석트래픽] (0) | 2021.07.21 |
2019 KAKAO BLIND RECRUITMENT_실패율 (3) | 2021.03.18 |
Comments