프로그래머스(C++) - 괄호 회전하기

2025. 10. 14. 15:15·C++ 프로그래머스 다이어리/Stack,Queue

●문제

●입출력

 

문제풀이

s에 있는  (, {. [가 바로 세트로 [] {} ()형식으로 닫히면 answer++

 

푼방법

제일 앞 글자를 구해서 그 문자를 한칸씩 회전시키면서 스택을 사용하여 올바르게 짝지어지는지 유효한지 검사

유효한 회전의 갯수를 카운트

#include <string>
#include <vector>
#include <stack>

using namespace std;

int solution(string s)
{
	int answer = 0;
	//s의 제일앞 원소를 구해서 제거한뒤 뒤로보낸다
	for (int i = 0; i < s.size(); i++)
	{
		string rotated = s;
		for (int j = 0; j < i; j++)
		{
			char firstChar = rotated.front();
			rotated.erase(0, 1);
			rotated.push_back(firstChar);
		}
		stack<char> checkStack;
		bool isValid = true;

		for (char c : rotated)
		{
			if (c == '(' || c == '[' || c == '{')
				checkStack.push(c);
			else
			{
				if (checkStack.empty())
				{
					isValid = false;
					break;
				}
				char top = checkStack.top();
				if ((c == ')' && top == '(') ||
					(c == ']' && top == '[') ||
					(c == '}' && top == '{'))
					checkStack.pop();
				else
				{
					isValid = false;
					break;
				}
			}
		}
		if (!checkStack.empty())
			isValid = false;
		if (isValid)
			answer++;
	}
	return answer;

}

 

레벨 : 2

점수 : 1

'C++ 프로그래머스 다이어리 > Stack,Queue' 카테고리의 다른 글

프로그래머스(C++) - 주식가격  (0) 2025.10.08
프로그래머스(C++) - 프로세스  (0) 2025.10.08
프로그래머스(C++) - 기능개발  (0) 2025.08.07
Algorithm - Stack, Queue  (0) 2025.08.07
프로그래머스(C++) - 짝지어 제거하기  (0) 2025.08.02
'C++ 프로그래머스 다이어리/Stack,Queue' 카테고리의 다른 글
  • 프로그래머스(C++) - 주식가격
  • 프로그래머스(C++) - 프로세스
  • 프로그래머스(C++) - 기능개발
  • Algorithm - Stack, Queue
lucodev
lucodev
커피와 노트북 그리고 개발
  • lucodev
    루코 개발테이블
    lucodev
  • 전체
    오늘
    어제
    • 분류 전체보기 (174) N
      • Unreal5 프로젝트 다이어리 (73)
      • Unreal5 프로젝트 다이어리2 (11)
      • Unreal 팁 (8)
      • Unreal 디버깅 (8)
      • 코드 개인보관함 (8)
      • C++ 프로그래머스 다이어리 (50) N
        • Stack,Queue (6)
        • Hash (4)
        • Heap (2)
        • Sort (5)
        • Exhaustive search (5)
        • Greedy (2)
        • BFS , DFS (6)
        • Graph (2)
        • Dynamic Programming (1)
        • C++ Math (2)
        • 기타 문제 (14) N
      • 코딩테스트+@ (11) N
      • 알고리즘 스타디 (1)
      • 알고리즘 스타디 과제 (3)
  • 인기 글

  • 최근 글

  • 최근 댓글

  • 링크

  • 공지사항

  • 블로그 메뉴

    • 홈
    • 태그
    • 방명록
  • 태그

    언리얼 behaviortree
    언리얼 foot step
    unreal loading
    언리얼 로딩창
    언리얼 페이드 아웃
    unreal sequence
    언리얼 모션매칭
    언리얼 비헤이비어트리
    언리얼 behavior tree
    unreal 모션매칭
    언리얼
    unreal 시퀀스
    언리얼 look at
    unreal 로딩
    언리얼 motionmatching
    언리얼 시퀀스
    unreal 컷씬
    unreal look at
    언리얼 컷씬
    언리얼 로딩
  • hELLO· Designed By정상우.v4.10.3
lucodev
프로그래머스(C++) - 괄호 회전하기
상단으로

티스토리툴바