vault backup: 2022-06-16 15:25:12

This commit is contained in:
juan 2022-06-16 15:25:12 +08:00
parent 7351064a87
commit bd15f3a069
3 changed files with 96 additions and 6 deletions

View file

@ -32,6 +32,6 @@
"repelStrength": 10, "repelStrength": 10,
"linkStrength": 1, "linkStrength": 1,
"linkDistance": 250, "linkDistance": 250,
"scale": 1.166549028490861, "scale": 0.9752800867986091,
"close": true "close": true
} }

View file

@ -19,17 +19,84 @@ tag:#stack
##### Links: ##### Links:
- [Link to problem]() - [Link to problem](https://leetcode.com/problems/valid-parentheses/)
___ ___
### Problem ### 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 #### Examples
**Example 1:**
```markdown
**Input:** s = "()"
**Output:** true
```
**Example 2:**
```markdown
**Input:** s = "()[]{}"
**Output:** true
```
**Example 3:**
```markdown
**Input:** s = "(]"
**Output:** false
```
#### Constraints #### Constraints
- `1 <= s.length <= 104`
- `s` consists of parentheses only `'()[]{}'`.
### Thoughts ### Thoughts
> [!summary] Basic stack usage.
> This is a #template_remove_me Can be implemented using std_stack or stc_vector.
obviously i should use std_stack.
### Solution ### Solution
```cpp
class Solution {
public:
bool isValid(string s) {
vector<char> stack;
vector<vector<char>> 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;
}
}
};
```

View file

@ -16,16 +16,39 @@
tag:#linked_list 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: ##### Links:
- [Link to problem]() - [Link to problem](https://leetcode.com/problems/remove-duplicates-from-sorted-list/)
___ ___
### Problem ### 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 #### 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 #### 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 ### Thoughts