vault backup: 2022-09-08 16:18:59
This commit is contained in:
parent
b128373fa8
commit
beb42d5156
|
@ -18,7 +18,7 @@ This is where I store notes about CS
|
||||||
#### Collection of problems of same kind
|
#### Collection of problems of same kind
|
||||||
|
|
||||||
```query
|
```query
|
||||||
#problem_collection
|
#problem_collection
|
||||||
```
|
```
|
||||||
|
|
||||||
#### TODO
|
#### TODO
|
||||||
|
|
|
@ -12,7 +12,7 @@
|
||||||
>
|
>
|
||||||
> ##### Difficulty:
|
> ##### Difficulty:
|
||||||
>
|
>
|
||||||
> #CS_analysis #difficulty_
|
> #CS*analysis #difficulty*
|
||||||
>
|
>
|
||||||
> ##### Additional tags:
|
> ##### Additional tags:
|
||||||
|
|
||||||
|
|
|
@ -3,10 +3,10 @@
|
||||||
2022-09-08 15:23
|
2022-09-08 15:23
|
||||||
|
|
||||||
#math
|
#math
|
||||||
#algorithm
|
#algorithm
|
||||||
#problem_collection
|
#problem_collection
|
||||||
|
|
||||||
These are problems solved by re-implementing elementary
|
These are problems solved by re-implementing elementary
|
||||||
math:
|
math:
|
||||||
|
|
||||||
### Add
|
### Add
|
||||||
|
@ -18,5 +18,4 @@ math:
|
||||||
|
|
||||||
### Multiply
|
### Multiply
|
||||||
|
|
||||||
|
### Divide
|
||||||
### Divide
|
|
||||||
|
|
|
@ -46,7 +46,7 @@ While traversing the linked list one of these things will occur-
|
||||||
- Move the fast pointer by **two** positions.
|
- Move the fast pointer by **two** positions.
|
||||||
- If both pointers meet at some point then a loop exists and if the fast pointer meets the end position then no loop exists.
|
- If both pointers meet at some point then a loop exists and if the fast pointer meets the end position then no loop exists.
|
||||||
|
|
||||||
Example:
|
Example:
|
||||||
[[Leetcode Linked-List-Cycle]]
|
[[Leetcode Linked-List-Cycle]]
|
||||||
|
|
||||||
#### Part 2. **Locating the start** of the loop
|
#### Part 2. **Locating the start** of the loop
|
||||||
|
@ -98,7 +98,7 @@ Let us consider an example:
|
||||||
- Move one step at a time until they met.
|
- Move one step at a time until they met.
|
||||||
- The start of the loop is where they met
|
- The start of the loop is where they met
|
||||||
|
|
||||||
Example:
|
Example:
|
||||||
[[Leetcode Linked-List-Cycle-II]]
|
[[Leetcode Linked-List-Cycle-II]]
|
||||||
|
|
||||||
#### Example
|
#### Example
|
||||||
|
|
|
@ -8,7 +8,7 @@
|
||||||
>
|
>
|
||||||
> ##### Data structures:
|
> ##### Data structures:
|
||||||
>
|
>
|
||||||
> #DS #linked_list
|
> #DS #linked_list
|
||||||
>
|
>
|
||||||
> ##### Difficulty:
|
> ##### Difficulty:
|
||||||
>
|
>
|
||||||
|
@ -16,7 +16,7 @@
|
||||||
>
|
>
|
||||||
> ##### Additional tags:
|
> ##### Additional tags:
|
||||||
>
|
>
|
||||||
> #leetcode
|
> #leetcode
|
||||||
>
|
>
|
||||||
> ##### Revisions:
|
> ##### Revisions:
|
||||||
>
|
>
|
||||||
|
@ -62,9 +62,9 @@ You may assume the two numbers do not contain any leading zero, except the numbe
|
||||||
|
|
||||||
#### Constraints
|
#### Constraints
|
||||||
|
|
||||||
- The number of nodes in each linked list is in the range `[1, 100]`.
|
- The number of nodes in each linked list is in the range `[1, 100]`.
|
||||||
- `0 <= Node.val <= 9`
|
- `0 <= Node.val <= 9`
|
||||||
- It is guaranteed that the list represents a number that does not have leading zeros.
|
- It is guaranteed that the list represents a number that does not have leading zeros.
|
||||||
|
|
||||||
### Thoughts
|
### Thoughts
|
||||||
|
|
||||||
|
@ -132,4 +132,4 @@ public:
|
||||||
return ans;
|
return ans;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
```
|
```
|
||||||
|
|
|
@ -12,7 +12,7 @@
|
||||||
>
|
>
|
||||||
> ##### Difficulty:
|
> ##### Difficulty:
|
||||||
>
|
>
|
||||||
> #coding_problem #difficulty_
|
> #coding*problem #difficulty*
|
||||||
>
|
>
|
||||||
> ##### Additional tags:
|
> ##### Additional tags:
|
||||||
>
|
>
|
||||||
|
|
152
OJ notes/pages/Leetcode Intersection-of-Two-Linked-Lists.md
Normal file
152
OJ notes/pages/Leetcode Intersection-of-Two-Linked-Lists.md
Normal file
|
@ -0,0 +1,152 @@
|
||||||
|
# Leetcode Intersection-of-Two-Linked-Lists
|
||||||
|
|
||||||
|
2022-09-08 16:13
|
||||||
|
|
||||||
|
> ##### Algorithms:
|
||||||
|
>
|
||||||
|
> #algorithm #two_pointers
|
||||||
|
>
|
||||||
|
> ##### Data structures:
|
||||||
|
>
|
||||||
|
> #DS #linked_list #set
|
||||||
|
>
|
||||||
|
> ##### Difficulty:
|
||||||
|
>
|
||||||
|
> #coding_problem #difficulty-easy
|
||||||
|
>
|
||||||
|
> ##### Additional tags:
|
||||||
|
>
|
||||||
|
> #leetcode
|
||||||
|
>
|
||||||
|
> ##### Revisions:
|
||||||
|
>
|
||||||
|
> N/A
|
||||||
|
|
||||||
|
##### Links:
|
||||||
|
|
||||||
|
- [Link to problem](https://leetcode.com/problems/intersection-of-two-linked-lists/)
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
### Problem
|
||||||
|
|
||||||
|
Given the heads of two singly linked-lists `headA` and `headB`, return _the node at which the two lists intersect_. If the two linked lists have no intersection at all, return `null`.
|
||||||
|
|
||||||
|
For example, the following two linked lists begin to intersect at node `c1`:
|
||||||
|
|
||||||
|
![](https://assets.leetcode.com/uploads/2021/03/05/160_statement.png)
|
||||||
|
|
||||||
|
The test cases are generated such that there are no cycles anywhere in the entire linked structure.
|
||||||
|
|
||||||
|
**Note** that the linked lists must **retain their original structure** after the function returns.
|
||||||
|
|
||||||
|
**Custom Judge:**
|
||||||
|
|
||||||
|
The inputs to the **judge** are given as follows (your program is **not** given these inputs):
|
||||||
|
|
||||||
|
- `intersectVal` - The value of the node where the intersection occurs. This is `0` if there is no intersected node.
|
||||||
|
- `listA` - The first linked list.
|
||||||
|
- `listB` - The second linked list.
|
||||||
|
- `skipA` - The number of nodes to skip ahead in `listA` (starting from the head) to get to the intersected node.
|
||||||
|
- `skipB` - The number of nodes to skip ahead in `listB` (starting from the head) to get to the intersected node.
|
||||||
|
|
||||||
|
The judge will then create the linked structure based on these inputs and pass the two heads, `headA` and `headB` to your program. If you correctly return the intersected node, then your solution will be **accepted**.
|
||||||
|
|
||||||
|
#### Examples
|
||||||
|
|
||||||
|
**Example 1:**
|
||||||
|
|
||||||
|
![](https://assets.leetcode.com/uploads/2021/03/05/160_example_1_1.png)
|
||||||
|
|
||||||
|
**Input:** intersectVal = 8, listA = [4,1,8,4,5], listB = [5,6,1,8,4,5], skipA = 2, skipB = 3
|
||||||
|
**Output:** Intersected at '8'
|
||||||
|
**Explanation:** The intersected node's value is 8 (note that this must not be 0 if the two lists intersect).
|
||||||
|
From the head of A, it reads as [4,1,8,4,5]. From the head of B, it reads as [5,6,1,8,4,5]. There are 2 nodes before the intersected node in A; There are 3 nodes before the intersected node in B.
|
||||||
|
|
||||||
|
- Note that the intersected node's value is not 1 because the nodes with value 1 in A and B (2nd node in A and 3rd node in B) are different node references. In other words, they point to two different locations in memory, while the nodes with value 8 in A and B (3rd node in A and 4th node in B) point to the same location in memory.
|
||||||
|
|
||||||
|
**Example 2:**
|
||||||
|
|
||||||
|
![](https://assets.leetcode.com/uploads/2021/03/05/160_example_2.png)
|
||||||
|
|
||||||
|
**Input:** intersectVal = 2, listA = [1,9,1,2,4], listB = [3,2,4], skipA = 3, skipB = 1
|
||||||
|
**Output:** Intersected at '2'
|
||||||
|
**Explanation:** The intersected node's value is 2 (note that this must not be 0 if the two lists intersect).
|
||||||
|
From the head of A, it reads as [1,9,1,2,4]. From the head of B, it reads as [3,2,4]. There are 3 nodes before the intersected node in A; There are 1 node before the intersected node in B.
|
||||||
|
|
||||||
|
**Example 3:**
|
||||||
|
|
||||||
|
![](https://assets.leetcode.com/uploads/2021/03/05/160_example_3.png)
|
||||||
|
|
||||||
|
**Input:** intersectVal = 0, listA = [2,6,4], listB = [1,5], skipA = 3, skipB = 2
|
||||||
|
**Output:** No intersection
|
||||||
|
**Explanation:** From the head of A, it reads as [2,6,4]. From the head of B, it reads as [1,5]. Since the two lists do not intersect, intersectVal must be 0, while skipA and skipB can be arbitrary values.
|
||||||
|
Explanation: The two lists do not intersect, so return null.
|
||||||
|
|
||||||
|
#### Constraints
|
||||||
|
|
||||||
|
- The number of nodes of `listA` is in the `m`.
|
||||||
|
- The number of nodes of `listB` is in the `n`.
|
||||||
|
- `1 <= m, n <= 3 * 104`
|
||||||
|
- `1 <= Node.val <= 105`
|
||||||
|
- `0 <= skipA < m`
|
||||||
|
- `0 <= skipB < n`
|
||||||
|
- `intersectVal` is `0` if `listA` and `listB` do not intersect.
|
||||||
|
- `intersectVal == listA[skipA] == listB[skipB]` if `listA` and `listB` intersect.
|
||||||
|
|
||||||
|
### Thoughts
|
||||||
|
|
||||||
|
> [!summary]
|
||||||
|
> This can be solved using #two_pointers or #set
|
||||||
|
|
||||||
|
#### Set
|
||||||
|
|
||||||
|
Record the address of visited node iterating over `listA`,
|
||||||
|
and verify if the address has been visited when iterating
|
||||||
|
over `listB`
|
||||||
|
|
||||||
|
#### Two pointers
|
||||||
|
|
||||||
|
#TODO Use two pointers method to solve
|
||||||
|
|
||||||
|
### Solution
|
||||||
|
|
||||||
|
#### Set
|
||||||
|
|
||||||
|
```cpp
|
||||||
|
/**
|
||||||
|
* Definition for singly-linked list.
|
||||||
|
* struct ListNode {
|
||||||
|
* int val;
|
||||||
|
* ListNode *next;
|
||||||
|
* ListNode(int x) : val(x), next(NULL) {}
|
||||||
|
* };
|
||||||
|
*/
|
||||||
|
class Solution {
|
||||||
|
public:
|
||||||
|
ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
|
||||||
|
// O(M + N), using set.
|
||||||
|
unordered_set<ListNode *> used;
|
||||||
|
|
||||||
|
ListNode *ptrA = headA;
|
||||||
|
|
||||||
|
while (ptrA) {
|
||||||
|
used.insert(ptrA);
|
||||||
|
ptrA = ptrA->next;
|
||||||
|
}
|
||||||
|
|
||||||
|
ListNode *ptrB = headB;
|
||||||
|
|
||||||
|
while (ptrB) {
|
||||||
|
if (used.find(ptrB) != used.end()) {
|
||||||
|
// intersect
|
||||||
|
return ptrB;
|
||||||
|
}
|
||||||
|
|
||||||
|
ptrB = ptrB->next;
|
||||||
|
}
|
||||||
|
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
```
|
|
@ -4,11 +4,11 @@
|
||||||
|
|
||||||
> ##### Algorithms:
|
> ##### Algorithms:
|
||||||
>
|
>
|
||||||
> #algorithm #Floyd_s_cycle_finding_algorithm
|
> #algorithm #Floyd_s_cycle_finding_algorithm
|
||||||
>
|
>
|
||||||
> ##### Data structures:
|
> ##### Data structures:
|
||||||
>
|
>
|
||||||
> #DS #linked_list
|
> #DS #linked_list
|
||||||
>
|
>
|
||||||
> ##### Difficulty:
|
> ##### Difficulty:
|
||||||
>
|
>
|
||||||
|
@ -16,7 +16,7 @@
|
||||||
>
|
>
|
||||||
> ##### Additional tags:
|
> ##### Additional tags:
|
||||||
>
|
>
|
||||||
> #leetcode
|
> #leetcode
|
||||||
>
|
>
|
||||||
> ##### Revisions:
|
> ##### Revisions:
|
||||||
>
|
>
|
||||||
|
@ -64,9 +64,9 @@ There is a cycle in a linked list if there is some node in the list that can be
|
||||||
|
|
||||||
#### Constraints
|
#### Constraints
|
||||||
|
|
||||||
- The number of the nodes in the list is in the range `[0, 104]`.
|
- The number of the nodes in the list is in the range `[0, 104]`.
|
||||||
- `-105 <= Node.val <= 105`
|
- `-105 <= Node.val <= 105`
|
||||||
- `pos` is `-1` or a **valid index** in the linked-list.
|
- `pos` is `-1` or a **valid index** in the linked-list.
|
||||||
|
|
||||||
### Thoughts
|
### Thoughts
|
||||||
|
|
||||||
|
@ -124,4 +124,4 @@ public:
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
```
|
```
|
||||||
|
|
|
@ -30,7 +30,7 @@
|
||||||
|
|
||||||
### Problem
|
### Problem
|
||||||
|
|
||||||
Given a string `s` which consists of lowercase or uppercase letters, return *the length of the **longest palindrome*** that can be built with those letters.
|
Given a string `s` which consists of lowercase or uppercase letters, return \*the length of the **longest palindrome\*** that can be built with those letters.
|
||||||
|
|
||||||
Letters are **case sensitive**, for example, `"Aa"` is not considered a palindrome here.
|
Letters are **case sensitive**, for example, `"Aa"` is not considered a palindrome here.
|
||||||
|
|
||||||
|
|
|
@ -61,7 +61,7 @@ In **Pascal's triangle**, each number is the sum of the two numbers directly abo
|
||||||
|
|
||||||
> [!summary]
|
> [!summary]
|
||||||
> This is a #recursion problem.
|
> This is a #recursion problem.
|
||||||
>
|
>
|
||||||
> Follow up of [[Leetcode Pascal's-Triangle]]
|
> Follow up of [[Leetcode Pascal's-Triangle]]
|
||||||
|
|
||||||
Because to get the nth row, we have to get the `n - 1` th
|
Because to get the nth row, we have to get the `n - 1` th
|
||||||
|
|
Loading…
Reference in a new issue