Notice
Recent Posts
Recent Comments
-
[2020_KAKAO] 괄호 변환 본문
https://programmers.co.kr/learn/courses/30/lessons/60058
문자열에 대해 활용할 수 있는 능력을 시험하는 문제였다.
위에서부터 1단계~를 구현하고 조건에 따라 재귀적으로 호출해주면서 값을 반환하는것이 관건이었다~
항상 단순화해서 생각하는 연습이 중요한 것 같다.
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
29
30
31
32
33
34
35
36
|
#include <string>
#include <vector>
using namespace std;
string solution(string w) {
string answer = "";
int left = 0, right = 0;
bool check = false;
if(w.length() == 0) return "";
for(int i=0; i<w.length(); i++){
if(w[i] == '(') left++;
if(w[i] == ')') right++;
if(right > left) check = true; // 올바른 문자열이 아닌경우
if(left == right){ // u가 만들어진 순간.
if(check){ // 균형잡힌 문자열인 경우
answer += "(";
answer += solution(w.substr(i+1));
answer += ")";
for(int j=1; j<i; j++){
if(w[j] == '(') answer += ")";
else if(w[j] == ')') answer += "(";
}
return answer;
}
else{
// 올바른 문자열인 경우
answer = answer + w.substr(0, i+1); // u
answer = answer + solution(w.substr(i+1));
// v를 1단계부터 넣은 결과 이어붙이기
return answer;
}
}
}
}
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
|
http://colorscripter.com/info#e" target="_blank" style="text-decoration:none;color:white">cs |
'1-3. 카카오 SW테스트' 카테고리의 다른 글
[KAKAO_2018] 캐시 (1차) (0) | 2020.02.18 |
---|---|
[KAKAO_2018] 프렌즈 4블록 (0) | 2020.02.14 |
[2019_KAKAO] 매칭 점수 (프로그래머스 레벨 3) (0) | 2020.02.12 |
[2017카카오코드본선] 단체사진 찍기 (0) | 2020.02.06 |
Comments