vault backup: 2022-07-10 08:29:59

This commit is contained in:
juan 2022-07-10 08:29:59 +08:00
parent deabec3eae
commit 7f90edd5c8
29 changed files with 192 additions and 3 deletions

View file

@ -32,6 +32,7 @@ tag:#CS_list_need_practicing
- [ ] [[$filename]]
```
- [ ] [[Leetcode Binary-Search]]
- [ ] [[Leetcode Binary-Tree-Postorder-Traversal]]
- [ ] [[Leetcode Reverse-Linked-List]]
- [ ] [[Leetcode Validate-Binary-Search-Tree]]
@ -47,12 +48,14 @@ tag:#leetcode
- [[$filename]]
```
- [[Leetcode Valid-Parentheses]]
- [[Leetcode Valid-Parentheses]]
- [[Leetcode Best-Time-To-Buy-And-Sell-Stock]]
- [[Leetcode Binary-Search]]
- [[Leetcode Binary-Tree-Inorder-Traversal]]
- [[Leetcode Binary-Tree-Level-Order-Traversal]]
- [[Leetcode Binary-Tree-Postorder-Traversal]]
- [[Leetcode Binary-Tree-Preorder-Traversal]]
- [[Leetcode First-Bad-Version]]
- [[Leetcode First-Unique-Character-In-a-String]]
- [[Leetcode Implement-Queue-Using-Stacks]]
- [[Leetcode Insert-Into-a-Binary-Search-Tree]]
@ -74,6 +77,7 @@ tag:#leetcode
- [[Leetcode Reverse-Linked-List]]
- [[Leetcode Search-a-2D-Matrix]]
- [[Leetcode Search-In-a-Binary-Tree]]
- [[Leetcode Search-Insert-Position]]
- [[Leetcode Symmetric-Tree]]
- [[Leetcode Two-Sum]]
- [[Leetcode Two-Sum-IV-Input-Is-a-BST]]
@ -103,12 +107,14 @@ tag:#DS tag:#coding_problem -tag:#template_remove_me
- [[$filename]]
```
- [[Leetcode Valid-Parentheses]]
- [[Leetcode Valid-Parentheses]]
- [[Leetcode Best-Time-To-Buy-And-Sell-Stock]]
- [[Leetcode Binary-Search]]
- [[Leetcode Binary-Tree-Inorder-Traversal]]
- [[Leetcode Binary-Tree-Level-Order-Traversal]]
- [[Leetcode Binary-Tree-Postorder-Traversal]]
- [[Leetcode Binary-Tree-Preorder-Traversal]]
- [[Leetcode First-Bad-Version]]
- [[Leetcode First-Unique-Character-In-a-String]]
- [[Leetcode Implement-Queue-Using-Stacks]]
- [[Leetcode Insert-Into-a-Binary-Search-Tree]]
@ -129,6 +135,7 @@ tag:#DS tag:#coding_problem -tag:#template_remove_me
- [[Leetcode Reverse-Linked-List]]
- [[Leetcode Search-a-2D-Matrix]]
- [[Leetcode Search-In-a-Binary-Tree]]
- [[Leetcode Search-Insert-Position]]
- [[Leetcode Symmetric-Tree]]
- [[Leetcode Two-Sum]]
- [[Leetcode Two-Sum-IV-Input-Is-a-BST]]
@ -160,10 +167,12 @@ tag:#algorithm tag:#coding_problem -tag:#template_remove_me
```
- [[Leetcode Best-Time-To-Buy-And-Sell-Stock]]
- [[Leetcode Binary-Search]]
- [[Leetcode Binary-Tree-Inorder-Traversal]]
- [[Leetcode Binary-Tree-Level-Order-Traversal]]
- [[Leetcode Binary-Tree-Postorder-Traversal]]
- [[Leetcode Binary-Tree-Preorder-Traversal]]
- [[Leetcode First-Bad-Version]]
- [[Leetcode First-Unique-Character-In-a-String]]
- [[Leetcode Insert-Into-a-Binary-Search-Tree]]
- [[Leetcode Intersection-of-Two-Arrays-II]]
@ -181,6 +190,7 @@ tag:#algorithm tag:#coding_problem -tag:#template_remove_me
- [[Leetcode Reverse-Linked-List]]
- [[Leetcode Search-a-2D-Matrix]]
- [[Leetcode Search-In-a-Binary-Tree]]
- [[Leetcode Search-Insert-Position]]
- [[Leetcode Symmetric-Tree]]
- [[Leetcode Two-Sum]]
- [[Leetcode Two-Sum-IV-Input-Is-a-BST]]

View file

@ -14,7 +14,12 @@ ___
tag:#coding_problem tag:#binary_search -tag:#template_remove_me
```
- [[Leetcode Binary-Search]]
- [[Leetcode First-Bad-Version]]
- [[Leetcode Lowest-Common-Ancestor-Of-a-Binary-Search-Tree]]
- [[Leetcode Search-a-2D-Matrix]]
- [[Leetcode Search-Insert-Position]]
- [[Leetcode Two-Sum-IV-Input-Is-a-BST]]
##### Links:

View file

@ -15,6 +15,7 @@ tag:#Kadane_s_algorithm
```
- [[Kadane's Algorithm]]
- [[Leetcode Maximum-Difference-Between-Increasing-Elements]]
- [[Leetcode Maxinum-subarray]]

View file

@ -19,8 +19,10 @@ tag:#binary_search
```
- [[Binary Search Algorithm]]
- [[Leetcode First-Bad-Version]]
- [[Leetcode Lowest-Common-Ancestor-Of-a-Binary-Search-Tree]]
- [[Leetcode Search-a-2D-Matrix]]
- [[Leetcode Search-Insert-Position]]
- [[Leetcode Two-Sum-IV-Input-Is-a-BST]]

View file

@ -22,6 +22,7 @@ tag:#DFS
- [[Leetcode Binary-Tree-Preorder-Traversal]]
- [[Leetcode Insert-Into-a-Binary-Search-Tree]]
- [[Leetcode Invert-Binary-Tree]]
- [[Leetcode Lowest-Common-Ancestor-Of-a-Binary-Search-Tree]]
- [[Leetcode Maximum-Depth-Of-Binary-Tree]]
- [[Leetcode Path-Sum]]
- [[Leetcode Search-In-a-Binary-Tree]]

View file

@ -18,6 +18,7 @@
tag:#BFS
```
- [[Breadth First Search]]
- [[Leetcode Maximum-Depth-Of-Binary-Tree]]
- [[Leetcode Search-In-a-Binary-Tree]]
- [[Leetcode Symmetric-Tree]]

View file

@ -22,6 +22,7 @@ tag:#DFS
- [[Leetcode Binary-Tree-Preorder-Traversal]]
- [[Leetcode Insert-Into-a-Binary-Search-Tree]]
- [[Leetcode Invert-Binary-Tree]]
- [[Leetcode Lowest-Common-Ancestor-Of-a-Binary-Search-Tree]]
- [[Leetcode Maximum-Depth-Of-Binary-Tree]]
- [[Leetcode Path-Sum]]
- [[Leetcode Search-In-a-Binary-Tree]]

View file

@ -22,6 +22,7 @@ tag:#DFS
- [[Leetcode Binary-Tree-Postorder-Traversal]]
- [[Leetcode Insert-Into-a-Binary-Search-Tree]]
- [[Leetcode Invert-Binary-Tree]]
- [[Leetcode Lowest-Common-Ancestor-Of-a-Binary-Search-Tree]]
- [[Leetcode Maximum-Depth-Of-Binary-Tree]]
- [[Leetcode Path-Sum]]
- [[Leetcode Search-In-a-Binary-Tree]]

View file

@ -18,6 +18,12 @@
tag:#binary_search
```
- [[Binary Search Algorithm]]
- [[Leetcode Binary-Search]]
- [[Leetcode Lowest-Common-Ancestor-Of-a-Binary-Search-Tree]]
- [[Leetcode Search-a-2D-Matrix]]
- [[Leetcode Search-Insert-Position]]
- [[Leetcode Two-Sum-IV-Input-Is-a-BST]]
##### Links:

View file

@ -16,6 +16,8 @@ tag:#string OR tag:#hash_table
- [[cpp_std_unordered_map]]
- [[Leetcode Ransom-Note]]
- [[Leetcode Two-Sum-IV-Input-Is-a-BST]]
- [[Leetcode Valid-Anagram]]
##### Links:

View file

@ -22,6 +22,7 @@ tag:#DFS
- [[Leetcode Binary-Tree-Postorder-Traversal]]
- [[Leetcode Binary-Tree-Preorder-Traversal]]
- [[Leetcode Invert-Binary-Tree]]
- [[Leetcode Lowest-Common-Ancestor-Of-a-Binary-Search-Tree]]
- [[Leetcode Maximum-Depth-Of-Binary-Tree]]
- [[Leetcode Path-Sum]]
- [[Leetcode Search-In-a-Binary-Tree]]

View file

@ -18,7 +18,9 @@ tag:#unordered_map OR tag:#two_pointers OR tag:#sort
- [[cpp_std_sort]]
- [[cpp_std_unordered_map]]
- [[Leetcode Merge-Sorted-Array]]
- [[Leetcode Merge-Two-Sorted-Lists]]
- [[Leetcode Two-Sum]]
- [[Two pointers approach]]

View file

@ -18,6 +18,7 @@
tag:#recursion tag:#DFS
```
- [[Leetcode Insert-Into-a-Binary-Search-Tree]]
- [[Leetcode Path-Sum]]
- [[Leetcode Symmetric-Tree]]

View file

@ -18,6 +18,7 @@
tag:#BFS
```
- [[Breadth First Search]]
- [[Leetcode Binary-Tree-Level-Order-Traversal]]
- [[Leetcode Search-In-a-Binary-Tree]]
- [[Leetcode Symmetric-Tree]]

View file

@ -19,6 +19,7 @@ tag:#Kadane_s_algorithm
- [[Kadane's Algorithm]]
- [[Leetcode Best-Time-To-Buy-And-Sell-Stock]]
- [[Leetcode Maximum-Difference-Between-Increasing-Elements]]

View file

@ -14,9 +14,15 @@
tag:#set OR tag:#multiset OR tag:#merge_sort OR tag:#vector OR tag:#two_pointers
```
- [[Binary Search Algorithm]]
- [[cpp_Range_based_for_loop]]
- [[cpp_std_multiset]]
- [[Leetcode Intersection-of-Two-Arrays-II]]
- [[Leetcode Merge-Two-Sorted-Lists]]
- [[Leetcode Reshape-The-Matrix]]
- [[Leetcode Search-a-2D-Matrix]]
- [[Leetcode Valid-Sudoku]]
- [[Two pointers approach]]

View file

@ -16,6 +16,7 @@ tag:#two_pointers
- [[Leetcode Intersection-of-Two-Arrays-II]]
- [[Leetcode Merge-Sorted-Array]]
- [[Two pointers approach]]
##### Links:

View file

@ -14,9 +14,22 @@
tag:#recursion OR tag:#iteration OR tag:#array -tag:#Kadane_s_algorithm
```
- [[Binary Search Algorithm]]
- [[cpp_Range_based_for_loop]]
- [[Leetcode Binary-Search]]
- [[Leetcode First-Bad-Version]]
- [[Leetcode First-Unique-Character-In-a-String]]
- [[Leetcode Insert-Into-a-Binary-Search-Tree]]
- [[Leetcode Invert-Binary-Tree]]
- [[Leetcode Path-Sum]]
- [[Leetcode Ransom-Note]]
- [[Leetcode Reshape-The-Matrix]]
- [[Leetcode Reverse-Linked-List]]
- [[Leetcode Search-Insert-Position]]
- [[Leetcode Symmetric-Tree]]
- [[Leetcode Two-Sum]]
- [[Leetcode Valid-Anagram]]
- [[Two pointers approach]]

View file

@ -23,6 +23,7 @@ tag:#DFS
- [[Leetcode Binary-Tree-Preorder-Traversal]]
- [[Leetcode Insert-Into-a-Binary-Search-Tree]]
- [[Leetcode Invert-Binary-Tree]]
- [[Leetcode Lowest-Common-Ancestor-Of-a-Binary-Search-Tree]]
- [[Leetcode Maximum-Depth-Of-Binary-Tree]]
- [[Leetcode Search-In-a-Binary-Tree]]
- [[Leetcode Symmetric-Tree]]

View file

@ -16,6 +16,8 @@ tag:#hash_table
- [[cpp_std_unordered_map]]
- [[Leetcode First-Unique-Character-In-a-String]]
- [[Leetcode Two-Sum-IV-Input-Is-a-BST]]
- [[Leetcode Valid-Anagram]]
##### Links:

View file

@ -12,11 +12,20 @@
(tag:#array OR tag:#vector) -tag:#Kadane_s_algorithm
```
- [[Binary Search Algorithm]]
- [[cpp_Range_based_for_loop]]
- [[Leetcode Binary-Search]]
- [[Leetcode First-Bad-Version]]
- [[Leetcode First-Unique-Character-In-a-String]]
- [[Leetcode Merge-Sorted-Array]]
- [[Leetcode Pascal's-Triangle]]
- [[Leetcode Ransom-Note]]
- [[Leetcode Search-a-2D-Matrix]]
- [[Leetcode Search-Insert-Position]]
- [[Leetcode Two-Sum]]
- [[Leetcode Valid-Anagram]]
- [[Leetcode Valid-Sudoku]]
- [[Two pointers approach]]

View file

@ -18,12 +18,14 @@
tag:#DFS OR tag:#BFS
```
- [[Breadth First Search]]
- [[Leetcode Binary-Tree-Inorder-Traversal]]
- [[Leetcode Binary-Tree-Level-Order-Traversal]]
- [[Leetcode Binary-Tree-Postorder-Traversal]]
- [[Leetcode Binary-Tree-Preorder-Traversal]]
- [[Leetcode Insert-Into-a-Binary-Search-Tree]]
- [[Leetcode Invert-Binary-Tree]]
- [[Leetcode Lowest-Common-Ancestor-Of-a-Binary-Search-Tree]]
- [[Leetcode Maximum-Depth-Of-Binary-Tree]]
- [[Leetcode Path-Sum]]
- [[Leetcode Symmetric-Tree]]

View file

@ -0,0 +1,103 @@
# Leetcode Search-Insert-Position
#### 2022-07-09 10:25
> ##### Algorithms:
> #algorithm #binary_search
> ##### Data structures:
> #DS #array
> ##### Difficulty:
> #coding_problem #difficulty-easy
> ##### Additional tags:
> #leetcode
> ##### Revisions:
> N/A
##### Related topics:
```expander
tag:#binary_search
```
- [[Binary Search Algorithm]]
- [[Leetcode Binary-Search]]
- [[Leetcode First-Bad-Version]]
- [[Leetcode Lowest-Common-Ancestor-Of-a-Binary-Search-Tree]]
- [[Leetcode Search-a-2D-Matrix]]
- [[Leetcode Two-Sum-IV-Input-Is-a-BST]]
##### Links:
- [Link to problem](https://leetcode.com/problems/binary-search/)
___
### Problem
Given an array of integers `nums` which is sorted in ascending order, and an integer `target`, write a function to search `target` in `nums`. If `target` exists, then return its index. Otherwise, return `-1`.
You must write an algorithm with `O(log n)` runtime complexity.
#### Examples
**Example 1:**
**Input:** nums = [-1,0,3,5,9,12], target = 9
**Output:** 4
**Explanation:** 9 exists in nums and its index is 4
**Example 2:**
**Input:** nums = [-1,0,3,5,9,12], target = 2
**Output:** -1
**Explanation:** 2 does not exist in nums so return -1
#### Constraints
- `1 <= nums.length <= 104`
- `-104 < nums[i], target < 104`
- All the integers in `nums` are **unique**.
- `nums` is sorted in ascending order.
### Thoughts
> [!summary]
> This is a #binary_search problem.
similar to [[Leetcode First-Bad-Version]], this relies on the converging nature of Bi-search
if the value is found, simply return it
if not found, at the end, position `l - 1` should be smaller than val.
and position `l` should be bigger, which is the position for the answer:
```
If not, return the index where it would be if it were inserted in order.
```
### Solution
Iteration
```cpp
class Solution {
public:
int searchInsert(vector<int>& nums, int target) {
// variant of bi-search
int r = nums.size() - 1;
int l = 0;
int mid, val;
do {
mid = l + (r - l) / 2;
val = nums[mid];
if (val == target) {
return mid;
} else if (val < target) {
l = mid + 1;
} else {
r = mid - 1;
}
} while (l <= r);
return l;
}
};
```

View file

@ -16,8 +16,13 @@ tag:#vector OR tag:#vector_2d OR tag:#binary_search
- [[Binary Search Algorithm]]
- [[cpp_Range_based_for_loop]]
- [[Leetcode Binary-Search]]
- [[Leetcode First-Bad-Version]]
- [[Leetcode Lowest-Common-Ancestor-Of-a-Binary-Search-Tree]]
- [[Leetcode Merge-Sorted-Array]]
- [[Leetcode Reshape-The-Matrix]]
- [[Leetcode Search-Insert-Position]]
- [[Leetcode Two-Sum-IV-Input-Is-a-BST]]
- [[Leetcode Valid-Sudoku]]

View file

@ -18,12 +18,14 @@
tag:#DFS OR tag:#BFS
```
- [[Breadth First Search]]
- [[Leetcode Binary-Tree-Inorder-Traversal]]
- [[Leetcode Binary-Tree-Level-Order-Traversal]]
- [[Leetcode Binary-Tree-Postorder-Traversal]]
- [[Leetcode Binary-Tree-Preorder-Traversal]]
- [[Leetcode Insert-Into-a-Binary-Search-Tree]]
- [[Leetcode Invert-Binary-Tree]]
- [[Leetcode Lowest-Common-Ancestor-Of-a-Binary-Search-Tree]]
- [[Leetcode Maximum-Depth-Of-Binary-Tree]]
- [[Leetcode Path-Sum]]
- [[Leetcode Search-In-a-Binary-Tree]]

View file

@ -19,10 +19,15 @@ tag:#BFS OR tag:#binary_search
```
- [[Binary Search Algorithm]]
- [[Breadth First Search]]
- [[Leetcode Binary-Search]]
- [[Leetcode Binary-Tree-Level-Order-Traversal]]
- [[Leetcode First-Bad-Version]]
- [[Leetcode Lowest-Common-Ancestor-Of-a-Binary-Search-Tree]]
- [[Leetcode Maximum-Depth-Of-Binary-Tree]]
- [[Leetcode Search-a-2D-Matrix]]
- [[Leetcode Search-In-a-Binary-Tree]]
- [[Leetcode Search-Insert-Position]]
- [[Leetcode Symmetric-Tree]]

View file

@ -1,4 +1,4 @@
# Leetcode Valid-Parentheses
# Leetcode Valid-Parentheses
#### 2022-06-16 14:50

View file

@ -12,9 +12,11 @@
tag:#vector
```
- [[Binary Search Algorithm]]
- [[cpp_Range_based_for_loop]]
- [[Leetcode Merge-Sorted-Array]]
- [[Leetcode Reshape-The-Matrix]]
- [[Leetcode Search-a-2D-Matrix]]
##### Links:

View file

@ -11,6 +11,8 @@ ___
```expander
tag:#coding_problem tag:#array tag:#vector tag:#multiset -tag:#template_remove_me
```
##### Links: