vault backup: 2022-07-03 09:58:52
This commit is contained in:
parent
23989ed98b
commit
e9be4598c4
|
@ -9,7 +9,7 @@
|
|||
> ##### Additional tags:
|
||||
> #leetcode
|
||||
> ##### Revisions:
|
||||
> Initial encounter: 2022-06-16
|
||||
> 1st revision: 2022-07-03 optimize code
|
||||
|
||||
##### Related topics:
|
||||
```expander
|
||||
|
@ -60,6 +60,41 @@ Basic stack usage.
|
|||
Can be implemented using std_stack or stc_vector.
|
||||
obviously i should use std_stack.
|
||||
### Solution
|
||||
1st revision: optimized code
|
||||
```cpp
|
||||
class Solution {
|
||||
public:
|
||||
bool isValid(string s) {
|
||||
stack<char> st;
|
||||
|
||||
char pairs[3][2] = {'(', ')', '{', '}', '[', ']'};
|
||||
const int pairSize = 3;
|
||||
|
||||
for (char c : s) {
|
||||
for (int i = 0; i < pairSize; i++) {
|
||||
if (pairs[i][0] == c) {
|
||||
// {([
|
||||
st.push(c);
|
||||
} else if (pairs[i][1] == c) {
|
||||
// ]})
|
||||
if (st.empty()) {
|
||||
// empty
|
||||
return false;
|
||||
} else if (st.top() != pairs[i][0]) {
|
||||
// mismatch
|
||||
return false;
|
||||
} else {
|
||||
st.pop();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return st.empty();
|
||||
}
|
||||
};
|
||||
|
||||
```
|
||||
|
||||
```cpp
|
||||
class Solution {
|
||||
|
@ -100,3 +135,4 @@ public:
|
|||
};
|
||||
|
||||
```
|
||||
|
||||
|
|
35
CS notes/pages/Leetcode Implement-Queue-Using-Stacks.md
Normal file
35
CS notes/pages/Leetcode Implement-Queue-Using-Stacks.md
Normal file
|
@ -0,0 +1,35 @@
|
|||
# Leetcode Implement-Queue-Using-Stacks
|
||||
|
||||
#### 2022-07-03 09:52
|
||||
|
||||
> ##### Data structures:
|
||||
> #DS #stack #queue
|
||||
> ##### Difficulty:
|
||||
> #coding_problem #difficulty-easy
|
||||
> ##### Additional tags:
|
||||
> #leetcode #CS_list_need_understanding
|
||||
> ##### Revisions:
|
||||
> N/A
|
||||
|
||||
##### Related topics:
|
||||
```expander
|
||||
tag:#stack tag:#queue
|
||||
```
|
||||
|
||||
|
||||
|
||||
##### Links:
|
||||
- [Link to problem]()
|
||||
___
|
||||
### Problem
|
||||
|
||||
#### Examples
|
||||
|
||||
#### Constraints
|
||||
|
||||
### Thoughts
|
||||
|
||||
> [!summary]
|
||||
> This is a #template_remove_me
|
||||
|
||||
### Solution
|
Loading…
Reference in a new issue