올바른 괄호

JJW·2024년 12월 13일

코딩 테스트

목록 보기
8/23

문제

Stack/Queuw > 올바른 괄호


문제 풀이

using System;
using System.Collections.Generic;

public class Solution 
{
    public bool solution(string s) 
    {
        Stack<char> stack = new Stack<char>();
        
        foreach(char c in s)
        {
            if(c == '(')
            {
                stack.Push(c);
            }
            else
            {
                // 추가되지도 않았는데 들어옴 ㅇㅇ
                if(stack.Count == 0)
                    return false;
                else
                {
                    stack.Pop();
                }                    
            }
        }
        
        return stack.Count == 0;
    }
}

느낀 점

처음엔 Queue로 풀었다가 작동도 안되고 이상해서 Stack으로 다시 풀었습니다.. Stack,Queue 없이 Count로도 가능은한데 Stack,Queue 문제라..ㅠ 공부의 필요성을 느낍니다.

profile
Unity 게임 개발자를 준비하는 취업준비생입니다..

0개의 댓글