Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 |
Tags
- LeetCode
- 리눅스
- mysql
- 우분투
- mariaDB
- while
- C++
- JungOl
- Linux
- publish
- Subscribe
- 프로그래밍
- MSG
- 오늘도 우라라 펫 공략
- 오늘도 우라라 공략
- 환경설정
- 오늘도 우라라
- topic
- install opencv-4.4.0 on ubuntu 22.04
- 데이터 베이스
- 토픽
- C언어
- 오늘도 우라라 펫
- 그랑사가
- 반복문
- ubuntu
- 등차수열
- 기초
- ros
- 마리아 DB
Archives
- Today
- Total
하루의 쉼터
[LeetCode] 20. Valid Parentheses 본문
반응형
Question :
Given a string s containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid.
An input string is valid if:
Open brackets must be closed by the same type of brackets.
Open brackets must be closed in the correct order.
Example 1:Input: s = "()"Output: true
Example 2:Input: s = "()[]{}"Output: true
Example 3:Input: s = "(]"Output: false
Example 4:Input: s = "([)]"Output: false
Example 5:Input: s = "{[]}"Output: true
Constraints:
1 <= s.length <= 104
s consists of parentheses only '()[]{}'.
Solution.h
#include<iostream>
#include<stack>
class Solution
{
private :
std::stack<char> stack_data;
public :
bool isValid(std::string s);
};
Solution.cpp
#include "Solution.h"
bool Solution::isValid(std::string s)
{
if (s.size() == 0)
return true;
else {
for (int loop_i = 0; loop_i < s.size(); loop_i++) {
if (s[loop_i] == '(' || s[loop_i] == '{' || s[loop_i] == '[') {
stack_data.push(s[loop_i]);
}
else {
if (stack_data.size() == 0)
return false;
switch (s[loop_i])
{
case ')':
if (stack_data.top() == '(') {
stack_data.pop();
break;
}
else
return false;
case '}':
if (stack_data.top() == '{') {
stack_data.pop();
break;
}
else
return false;
case ']':
if (stack_data.top() == '[') {
stack_data.pop();
break;
}
else
return false;
default:
break;
}
}
}
return (stack_data.size() == 0) ? true : false;
}
}
Result :
![]() |
Git Hub :
Anchangun - Overview
어제보다 나은 오늘. Anchangun has 3 repositories available. Follow their code on GitHub.
github.com
반응형
'Coding Test > LeetCode' 카테고리의 다른 글
[LeetCode] 53. Maximum Subarray (0) | 2020.11.30 |
---|---|
[LeetCode] 21. Merge Two Sorted Lists (0) | 2020.11.24 |
[LeetCode] 136. Single Number (0) | 2020.11.23 |
[LeetCode] 1. Two Sum (0) | 2020.11.17 |
[LeetCode] 288. Add Digits (0) | 2020.11.16 |
Comments