diff --git a/.obsidian/graph.json b/.obsidian/graph.json index 5321fcd..4144d8b 100644 --- a/.obsidian/graph.json +++ b/.obsidian/graph.json @@ -32,6 +32,6 @@ "repelStrength": 10, "linkStrength": 1, "linkDistance": 250, - "scale": 1.166549028490861, + "scale": 0.9752800867986091, "close": true } \ No newline at end of file diff --git a/CS notes/pages/Leetcode Valid-Parentheses.md b/CS notes/pages/Leetcode Valid-Parentheses.md index ee0a22a..a9ee0e9 100644 --- a/CS notes/pages/Leetcode Valid-Parentheses.md +++ b/CS notes/pages/Leetcode Valid-Parentheses.md @@ -19,17 +19,84 @@ tag:#stack ##### Links: -- [Link to problem]() +- [Link to problem](https://leetcode.com/problems/valid-parentheses/) ___ ### Problem +Given a string `s` containing just the characters `'('`, `')'`, `'{'`, `'}'`, `'['` and `']'`, determine if the input string is valid. + +An input string is valid if: + +1. Open brackets must be closed by the same type of brackets. +2. Open brackets must be closed in the correct order. #### Examples +**Example 1:** +```markdown +**Input:** s = "()" +**Output:** true +``` + +**Example 2:** + +```markdown +**Input:** s = "()[]{}" +**Output:** true +``` + +**Example 3:** + +```markdown +**Input:** s = "(]" +**Output:** false +``` #### Constraints +- `1 <= s.length <= 104` +- `s` consists of parentheses only `'()[]{}'`. ### Thoughts -> [!summary] -> This is a #template_remove_me - +Basic stack usage. +Can be implemented using std_stack or stc_vector. +obviously i should use std_stack. ### Solution + +```cpp +class Solution { +public: + bool isValid(string s) { + vector stack; + vector> pairs = {{'(', ')'}, {'{', '}'}, {'[', ']'}}; + + int loc; + const int size = pairs.size(); + for (char c : s) { + + for (loc = 0; loc < size; loc++) { + if (c == pairs[loc][1]) { + if (stack.empty()) { + // if there is no {([ before + printf("No {([ before.\n"); + return false; + } else if (stack.back() == pairs[loc][0]) { + stack.pop_back(); + } else { + // parens mismatch + printf("parens mismatch: %c %c\n", stack.back(), pairs[loc][0]); + return false; + } + } else if (c == pairs[loc][0]) { + stack.push_back(c); + } + } + } + + if (stack.empty()) { + return true; + } else { + return false; + } + } +}; + +``` \ No newline at end of file diff --git a/CS notes/pages/Leetcode Remove-Duplicates-From-Sorted-List.md b/CS notes/pages/Leetcode Remove-Duplicates-From-Sorted-List.md index 2113ce7..6e0cba8 100644 --- a/CS notes/pages/Leetcode Remove-Duplicates-From-Sorted-List.md +++ b/CS notes/pages/Leetcode Remove-Duplicates-From-Sorted-List.md @@ -16,16 +16,39 @@ tag:#linked_list ``` +- [[Floyd's Cycle Finding Algorithm]] +- [[Leetcode Linked-List-Cycle]] +- [[Leetcode Merge-Two-Sorted-Lists]] +- [[Leetcode Remove-Linked-List-Elements]] +- [[Leetcode Reverse-Linked-List]] +- [[Two pointers approach]] ##### Links: -- [Link to problem]() +- [Link to problem](https://leetcode.com/problems/remove-duplicates-from-sorted-list/) ___ ### Problem +Given the `head` of a sorted linked list, _delete all duplicates such that each element appears only once_. Return _the linked list **sorted** as well_. #### Examples +**Example 1:** + +![](https://assets.leetcode.com/uploads/2021/01/04/list1.jpg) + +**Input:** head = [1,1,2] +**Output:** [1,2] + +**Example 2:** + +![](https://assets.leetcode.com/uploads/2021/01/04/list2.jpg) + +**Input:** head = [1,1,2,3,3] +**Output:** [1,2,3] #### Constraints +- The number of nodes in the list is in the range `[0, 300]`. +- `-100 <= Node.val <= 100` +- The list is guaranteed to be **sorted** in ascending order. ### Thoughts