[LeetCode]유효한 괄호

Inhwan98·2023년 3월 30일
0

PTU STUDY_leetcode

목록 보기
19/24

문제

괄호로 된 입력값이 올바른지 판단하라.

예제

  • Example 1:
Input: s = "()"
Output: true
  • Example 2:
Input: s = "()[]{}"
Output: true
  • Example 3:
Input: s = "(]"
Output: false

코드

class Solution {
public:
    bool isValid(string s) {

    stack<char> sc;
	map<char, char> m;
	bool ans = true;

	m.insert({ ')', '(' });
	m.insert({ ']', '[' });
	m.insert({ '}', '{' });

 	for (int i = 0; i < s.size(); i++)
	{
		sc.push(s[i]);
		auto a = m.find(sc.top());
		
		if (a != m.end())
		{
			sc.pop();
			if (!sc.empty())
			{
				if (a->second == sc.top()) sc.pop();
				else
				{
					ans = false;
					break;
				}
			}
			else
			{
				ans = false;
				break;
			}
		}
	}

    if (!sc.empty()) ans = false;

    return ans;

    }
};

결과

Runtime 0 ms / Memory 6.4 MB
https://leetcode.com/problems/valid-parentheses/submissions/925086095/


https://leetcode.com/problems/valid-parentheses/

profile
코딩마스터

0개의 댓글