1️⃣ C++ 코딩테스트 공부
짝이 있을 때는 unorderded_map을 사용하여 탐색의 기준을 키로 저장하면 좋다.
🔹 10 괄호 회전하기
#include <string>
#include <vector>
#include <stack>
#include <iostream>
using namespace std;
int IsRight(const string& str)
{
stack<char> st;
for(char ch : str)
{
if(!st.empty()) //이걸 안 넣어주면 core dumped 에러 발생!
{
if(ch == ')')
{
if(st.top()=='(')
{
st.pop();
continue;
}
}
else if(ch == '}')
{
if(st.top()=='{')
{
st.pop();
continue;
}
}
else if(ch == ']')
{
if(st.top()=='[')
{
st.pop();
continue;
}
}
}
st.push(ch);
}
return st.empty() ? 1 : 0;
}
int solution(string s) {
int answer = 0;
for(int i = 0; i<s.size(); i++)
{
string news ="";
for(int j =0; j<s.size();j++)
{
news+= s[(i+j)%s.size()];
}
answer += IsRight(news);
}
return answer;
}
🔹 11 짝지어 제거하기
#include <iostream>
#include <string>
#include <stack>
using namespace std;
int solution(string s)
{
stack<char> st;
for(char ch : s)
{
if(!st.empty())
{
if(ch == st.top())
{
st.pop();
continue;
}
}
st.push(ch);
}
return st.empty() //? 1 : 0; 없어도 되는 부분
}
'TIL' 카테고리의 다른 글
[250313 TIL] 뭔가 많이 쓸 것 같은 어댑터 패턴 (1) | 2025.03.13 |
---|---|
[250312 TIL] 팩토리 패턴_디자인 패턴 공부로 포인터 다시 공부 (0) | 2025.03.12 |
[250311 TIL] 언리얼 에디터 깃허브 소스 빌드 너무 오래 걸려... (0) | 2025.03.11 |
[250310 TIL] 네트워크 개념 (0) | 2025.03.10 |
[250226 TIL] 코드는 타이밍이 생명 (0) | 2025.02.26 |