본문 바로가기
Coding Test/Programmers

[Programmers] (월간 코드 챌린지 시즌2) Lv 2. 괄호 회전하기

by song.ift 2023. 6. 10.

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

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

 

bash
닫기
#include <string> #include <vector> #include <stack> using namespace std; int solution(string s) { ​​​​int answer = 0; ​​​​ ​​​​for (auto iter = s.begin(); iter != s.end(); ++iter) ​​​​{ ​​​​​​​​string temp = s; ​​​​​​​​stack<char> st; // 후입선출로 괄호가 올바른지 체크 ​​​​​​​​int start = iter - s.begin(); // iter에서 begin과의 차이가 회전시킬 문자의 수 ​​​​​​​​temp.erase(0, start); // 앞에서부터 삭제 ​​​​​​​​temp += s.substr(0, start); // 맨 뒤에 붙여줌 ​​​​​​​​for (const auto word : temp) ​​​​​​​​{ ​​​​​​​​​​​​if (st.empty()) ​​​​​​​​​​​​​​​​st.push(word); ​​​​​​​​​​​​else if ((st.top() == '[' && word == ']') ​​​​​​​​​​​​​​​​|| (st.top() == '(' && word == ')') ​​​​​​​​​​​​​​​​|| (st.top() == '{' && word == '}')) ​​​​​​​​​​​​​​​​st.pop(); ​​​​​​​​​​​​else if (word == ']' || word == ')' || word == '}') break; // 닫는 괄호일 경우, 뒤에 더이상 체크할 필요가 없어서 break; ​​​​​​​​​​​​else st.push(word); ​​​​​​​​}; ​​​​​​​​if (st.empty()) // 비어있으면 올바르다고 판단 ​​​​​​​​​​​​++answer; ​​​​} ​​​​return answer; }

GitHub : https://github.com/developeSHG/Algorithm-Baekjoon_Programmers/blob/c3c138cea2f5056654df49f5275335c079b2651d/%ED%94%84%EB%A1%9C%EA%B7%B8%EB%9E%98%EB%A8%B8%EC%8A%A4/lv2/76502.%E2%80%85%EA%B4%84%ED%98%B8%E2%80%85%ED%9A%8C%EC%A0%84%ED%95%98%EA%B8%B0/%EA%B4%84%ED%98%B8%E2%80%85%ED%9A%8C%EC%A0%84%ED%95%98%EA%B8%B0.cpp

 

GitHub - developeSHG/Algorithm-Baekjoon_Programmers: 백준 and 프로그래머스 소스코드

백준 and 프로그래머스 소스코드. Contribute to developeSHG/Algorithm-Baekjoon_Programmers development by creating an account on GitHub.

github.com

 

댓글