공부/C#

[C#] 프로그래머스 스택/큐 - 올바른 괄호

굴러다니다니 2025. 3. 9. 21:13
728x90

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

 

프로그래머스

SW개발자를 위한 평가, 교육, 채용까지 Total Solution을 제공하는 개발자 성장을 위한 베이스캠프

programmers.co.kr

 

using System;
using System.Collections.Generic;

public class Solution {
    public bool solution(string s) {
        Stack<char> stack = new Stack<char>();
        foreach(var a in s){
            if (a == '('){
                stack.Push(a);
            }
            else if (a == ')'){
                if (stack.Count < 1) return false;
                stack.Pop();
            }
        }
        if (stack.Count == 0) return true;
        else return false;
    }
}

예전에 수업때 c++로 스택 안쓰고 풀었었는데, 스택쓰니까 진짜 빨리 풀었다...

스택 최고

728x90