vault backup: 2022-06-16 15:25:12
This commit is contained in:
parent
7351064a87
commit
bd15f3a069
2
.obsidian/graph.json
vendored
2
.obsidian/graph.json
vendored
|
@ -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
|
||||||
}
|
}
|
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
```
|
|
@ -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:**
|
||||||
|
|
||||||
|

|
||||||
|
|
||||||
|
**Input:** head = [1,1,2]
|
||||||
|
**Output:** [1,2]
|
||||||
|
|
||||||
|
**Example 2:**
|
||||||
|
|
||||||
|

|
||||||
|
|
||||||
|
**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
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue