FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
LeetCode-Solutions/C++/valid-parentheses.cpp at master · spotpan/LeetCode-Solutions · GitHub
spotpan
LeetCode-Solutions
Repository navigation
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
LeetCode-Solutions
/
C++
/
valid-parentheses.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
25 lines (24 loc) · 767 Bytes
Breadcrumbs
LeetCode-Solutions
/
C++
/
valid-parentheses.cpp
Copy path
File metadata and controls
25 lines (24 loc) · 767 Bytes
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
//
Time: O(n)
//
Space: O(n)
class
Solution
{
public:
bool
isValid
(string s) {
const
unordered_map<
char
,
char
> symbol_pair = {{
'
)
'
,
'
(
'
},
{
'
]
'
,
'
[
'
},
{
'
}
'
,
'
{
'
}};
stack<
char
> parentheses;
for
(
const
auto
& c: s) {
const
auto
& it = symbol_pair.
find
(c);
if
(it != symbol_pair.
cend
()) {
if
(parentheses.
empty
() ||
parentheses.
top
() != it->
second
) {
return
false
;
}
parentheses.
pop
();
}
else
{
parentheses.
emplace
(c);
}
}
return
parentheses.
empty
();
}
};
Back
|
FazBrowse Home
|
New Git URL