From e9be4598c402381879ef6cc310a11f8dfd651a74 Mon Sep 17 00:00:00 2001 From: juan Date: Sun, 3 Jul 2022 09:58:52 +0800 Subject: [PATCH] vault backup: 2022-07-03 09:58:52 --- CS notes/pages/Leetcode Valid-Parentheses.md | 40 ++++++++++++++++++- .../Leetcode Implement-Queue-Using-Stacks.md | 35 ++++++++++++++++ 2 files changed, 73 insertions(+), 2 deletions(-) create mode 100644 CS notes/pages/Leetcode Implement-Queue-Using-Stacks.md diff --git a/CS notes/pages/Leetcode Valid-Parentheses.md b/CS notes/pages/Leetcode Valid-Parentheses.md index a9ee0e9..10dc50e 100644 --- a/CS notes/pages/Leetcode Valid-Parentheses.md +++ b/CS notes/pages/Leetcode Valid-Parentheses.md @@ -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 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 { @@ -99,4 +134,5 @@ public: } }; -``` \ No newline at end of file +``` + diff --git a/CS notes/pages/Leetcode Implement-Queue-Using-Stacks.md b/CS notes/pages/Leetcode Implement-Queue-Using-Stacks.md new file mode 100644 index 0000000..b1eaedf --- /dev/null +++ b/CS notes/pages/Leetcode Implement-Queue-Using-Stacks.md @@ -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