-
Notifications
You must be signed in to change notification settings - Fork 0
/
Day 24 Valid parantheses.cpp
61 lines (50 loc) · 1.14 KB
/
Day 24 Valid parantheses.cpp
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
//problem 1 Valid paarantheses leet code ************************************//
class Solution {
public:
bool isValid(string s) {
stack<char> st;
st.push(s[0]);
for(int i = 1; i<s.size(); i++){
if( s[i] == ')' && !st.empty() && st.top() == '(' ) st.pop();
else if( s[i] == '}' && !st.empty() && st.top() == '{' ) st.pop();
else if( s[i] == ']' && !st.empty() && st.top() == '[' ) st.pop();
else st.push(s[i]);
}
return st.empty();
}
}
//***problem 2 Implement Queue using stack.cpp///
class MyQueue {
public:
stack<int>st;
stack<int>a;
MyQueue() {
}
void push(int x)
{
while(!st.empty())
{
int t=st.top();
st.pop();
a.push(t);
}
a.push(x);
while(!a.empty())
{
int t=a.top();
a.pop();
st.push(t);
}
}
int pop() {
int r=st.top();
st.pop();
return r;
}
int peek() {
return st.top();
}
bool empty() {
return st.empty();
}
};