Skip to content

Commit ef45415

Browse files
committed
valid parentheses ์ถ”๊ฐ€
1 parent 923dfdd commit ef45415

File tree

1 file changed

+99
-0
lines changed

1 file changed

+99
-0
lines changed
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
# ์—ฐ๊ด€ ๋งํฌ
2+
- [๋ฌธ์ œ ํ’€์ด ์Šค์ผ€์ค„](https://github.com/orgs/DaleStudy/projects/6/views/5)
3+
- [๋‹ต์•ˆ ์ฝ”๋“œ ์ œ์ถœ๋ฒ•](https://github.com/DaleStudy/leetcode-study/wiki/%EB%8B%B5%EC%95%88-%EC%A0%9C%EC%B6%9C-%EA%B0%80%EC%9D%B4%EB%93%9C)
4+
5+
# Problem
6+
- ๋ฌธ์ œ ๋งํฌ : https://leetcode.com/problems/valid-parentheses/description/
7+
- ๋ฌธ์ œ ์ด๋ฆ„ : valid parentheses
8+
- ๋ฌธ์ œ ๋ฒˆํ˜ธ : 20
9+
- ๋‚œ์ด๋„ : easy
10+
- ์นดํ…Œ๊ณ ๋ฆฌ :
11+
12+
# ๋ฌธ์ œ ์„ค๋ช…
13+
14+
15+
16+
# ์•„์ด๋””์–ด
17+
- stack์„ ํ†ตํ•œ ์œ ํšจ์„ฑ ์ฒดํฌ
18+
- brute force
19+
20+
# โœ… ์ฝ”๋“œ (Solution)
21+
22+
23+
```cpp
24+
class Solution {
25+
public:
26+
bool isValid(string s) {
27+
stack<char> st;
28+
for(int i=0;i<s.size();i++){
29+
auto cur = s[i];
30+
if(cur == '(' || cur == '{' || cur == '['){
31+
st.push(cur);
32+
continue;
33+
}else{
34+
if(st.size()==0){
35+
return false;
36+
}
37+
if(cur == ']'){
38+
if(st.top() == '['){
39+
st.pop();
40+
continue;
41+
}else{
42+
return false;
43+
}
44+
}else if(cur == '}'){
45+
if(st.top()=='{'){
46+
st.pop();
47+
continue;
48+
}else{
49+
return false;
50+
}
51+
}else {
52+
if(st.top()=='('){
53+
st.pop();
54+
continue;
55+
}else{
56+
return false;
57+
}
58+
}
59+
}
60+
}
61+
return st.size()==0;
62+
}
63+
};
64+
65+
66+
```
67+
68+
### ๊ฐ„๋žตํ™”
69+
```cpp
70+
class Solution {
71+
public:
72+
bool isValid(string s) {
73+
stack<char> st;
74+
for (char c : s) {
75+
if (c == '(') st.push(')');
76+
else if (c == '{') st.push('}');
77+
else if (c == '[') st.push(']');
78+
else {
79+
if (st.empty() || st.top() != c) return false;
80+
st.pop();
81+
}
82+
}
83+
return st.empty();
84+
}
85+
};
86+
```
87+
88+
# ๐Ÿ” ์ฝ”๋“œ ์„ค๋ช…
89+
90+
91+
# ์ตœ์ ํ™” ํฌ์ธํŠธ (Optimality Discussion)
92+
- ๊น”๋”ํ•˜๊ฒŒ ๋งŒ๋“ฆ
93+
# ๐Ÿงช ํ…Œ์ŠคํŠธ & ์—ฃ์ง€ ์ผ€์ด์Šค
94+
95+
# ๐Ÿ“š ๊ด€๋ จ ์ง€์‹ ๋ณต์Šต
96+
97+
# ๐Ÿ” ํšŒ๊ณ 
98+
99+

0 commit comments

Comments
ย (0)