From 7f90edd5c889a71a806419ad7bda6972b55566b7 Mon Sep 17 00:00:00 2001 From: juan Date: Sun, 10 Jul 2022 08:29:59 +0800 Subject: [PATCH] vault backup: 2022-07-10 08:29:59 --- OJ notes/OJ-index.md | 14 ++- OJ notes/pages/Binary Search Algorithm.md | 5 + ...eetcode Best-Time-To-Buy-And-Sell-Stock.md | 1 + OJ notes/pages/Leetcode Binary-Search.md | 2 + .../Leetcode Binary-Tree-Inorder-Traversal.md | 1 + ...tcode Binary-Tree-Level-Order-Traversal.md | 1 + ...eetcode Binary-Tree-Postorder-Traversal.md | 1 + ...Leetcode Binary-Tree-Preorder-Traversal.md | 1 + OJ notes/pages/Leetcode First-Bad-Version.md | 6 + ...code First-Unique-Character-In-a-String.md | 2 + ...etcode Insert-Into-a-Binary-Search-Tree.md | 1 + .../Leetcode Intersection-of-Two-Arrays-II.md | 2 + OJ notes/pages/Leetcode Invert-Binary-Tree.md | 1 + .../Leetcode Maximum-Depth-Of-Binary-Tree.md | 1 + OJ notes/pages/Leetcode Maxinum-subarray.md | 1 + OJ notes/pages/Leetcode Merge-Sorted-Array.md | 6 + .../pages/Leetcode Merge-Two-Sorted-Lists.md | 1 + OJ notes/pages/Leetcode Pascal's-Triangle.md | 13 +++ OJ notes/pages/Leetcode Path-Sum.md | 1 + OJ notes/pages/Leetcode Ransom-Note.md | 2 + OJ notes/pages/Leetcode Reshape-The-Matrix.md | 9 ++ .../pages/Leetcode Search-In-a-Binary-Tree.md | 2 + .../pages/Leetcode Search-Insert-Position.md | 103 ++++++++++++++++++ OJ notes/pages/Leetcode Search-a-2D-Matrix.md | 5 + OJ notes/pages/Leetcode Symmetric-Tree.md | 2 + .../Leetcode Two-Sum-IV-Input-Is-a-BST.md | 5 + ...heses.md => Leetcode Valid-Parentheses.md} | 2 +- OJ notes/pages/Leetcode Valid-Sudoku.md | 2 + OJ notes/pages/cpp_Range_based_for_loop.md | 2 + 29 files changed, 192 insertions(+), 3 deletions(-) create mode 100644 OJ notes/pages/Leetcode Search-Insert-Position.md rename OJ notes/pages/{Leetcode Valid-Parentheses.md => Leetcode Valid-Parentheses.md} (98%) diff --git a/OJ notes/OJ-index.md b/OJ notes/OJ-index.md index c1afdcb..8b91ebf 100644 --- a/OJ notes/OJ-index.md +++ b/OJ notes/OJ-index.md @@ -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]] diff --git a/OJ notes/pages/Binary Search Algorithm.md b/OJ notes/pages/Binary Search Algorithm.md index ed99a01..a022c42 100644 --- a/OJ notes/pages/Binary Search Algorithm.md +++ b/OJ notes/pages/Binary Search Algorithm.md @@ -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: diff --git a/OJ notes/pages/Leetcode Best-Time-To-Buy-And-Sell-Stock.md b/OJ notes/pages/Leetcode Best-Time-To-Buy-And-Sell-Stock.md index 4f0c148..24777b5 100644 --- a/OJ notes/pages/Leetcode Best-Time-To-Buy-And-Sell-Stock.md +++ b/OJ notes/pages/Leetcode Best-Time-To-Buy-And-Sell-Stock.md @@ -15,6 +15,7 @@ tag:#Kadane_s_algorithm ``` - [[Kadane's Algorithm]] +- [[Leetcode Maximum-Difference-Between-Increasing-Elements]] - [[Leetcode Maxinum-subarray]] diff --git a/OJ notes/pages/Leetcode Binary-Search.md b/OJ notes/pages/Leetcode Binary-Search.md index 97b0f5d..13522cc 100644 --- a/OJ notes/pages/Leetcode Binary-Search.md +++ b/OJ notes/pages/Leetcode Binary-Search.md @@ -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]] diff --git a/OJ notes/pages/Leetcode Binary-Tree-Inorder-Traversal.md b/OJ notes/pages/Leetcode Binary-Tree-Inorder-Traversal.md index 9b89ddc..c9f308a 100644 --- a/OJ notes/pages/Leetcode Binary-Tree-Inorder-Traversal.md +++ b/OJ notes/pages/Leetcode Binary-Tree-Inorder-Traversal.md @@ -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]] diff --git a/OJ notes/pages/Leetcode Binary-Tree-Level-Order-Traversal.md b/OJ notes/pages/Leetcode Binary-Tree-Level-Order-Traversal.md index 42507bb..6e54b96 100644 --- a/OJ notes/pages/Leetcode Binary-Tree-Level-Order-Traversal.md +++ b/OJ notes/pages/Leetcode Binary-Tree-Level-Order-Traversal.md @@ -18,6 +18,7 @@ tag:#BFS ``` +- [[Breadth First Search]] - [[Leetcode Maximum-Depth-Of-Binary-Tree]] - [[Leetcode Search-In-a-Binary-Tree]] - [[Leetcode Symmetric-Tree]] diff --git a/OJ notes/pages/Leetcode Binary-Tree-Postorder-Traversal.md b/OJ notes/pages/Leetcode Binary-Tree-Postorder-Traversal.md index d42bcd0..8502a00 100644 --- a/OJ notes/pages/Leetcode Binary-Tree-Postorder-Traversal.md +++ b/OJ notes/pages/Leetcode Binary-Tree-Postorder-Traversal.md @@ -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]] diff --git a/OJ notes/pages/Leetcode Binary-Tree-Preorder-Traversal.md b/OJ notes/pages/Leetcode Binary-Tree-Preorder-Traversal.md index b8ae878..da5741f 100644 --- a/OJ notes/pages/Leetcode Binary-Tree-Preorder-Traversal.md +++ b/OJ notes/pages/Leetcode Binary-Tree-Preorder-Traversal.md @@ -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]] diff --git a/OJ notes/pages/Leetcode First-Bad-Version.md b/OJ notes/pages/Leetcode First-Bad-Version.md index 3aff7e6..095960b 100644 --- a/OJ notes/pages/Leetcode First-Bad-Version.md +++ b/OJ notes/pages/Leetcode First-Bad-Version.md @@ -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: diff --git a/OJ notes/pages/Leetcode First-Unique-Character-In-a-String.md b/OJ notes/pages/Leetcode First-Unique-Character-In-a-String.md index b44f0f1..4c1b3e4 100644 --- a/OJ notes/pages/Leetcode First-Unique-Character-In-a-String.md +++ b/OJ notes/pages/Leetcode First-Unique-Character-In-a-String.md @@ -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: diff --git a/OJ notes/pages/Leetcode Insert-Into-a-Binary-Search-Tree.md b/OJ notes/pages/Leetcode Insert-Into-a-Binary-Search-Tree.md index 52b0cc3..8df40d5 100644 --- a/OJ notes/pages/Leetcode Insert-Into-a-Binary-Search-Tree.md +++ b/OJ notes/pages/Leetcode Insert-Into-a-Binary-Search-Tree.md @@ -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]] diff --git a/OJ notes/pages/Leetcode Intersection-of-Two-Arrays-II.md b/OJ notes/pages/Leetcode Intersection-of-Two-Arrays-II.md index f19fe67..ab24631 100644 --- a/OJ notes/pages/Leetcode Intersection-of-Two-Arrays-II.md +++ b/OJ notes/pages/Leetcode Intersection-of-Two-Arrays-II.md @@ -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]] diff --git a/OJ notes/pages/Leetcode Invert-Binary-Tree.md b/OJ notes/pages/Leetcode Invert-Binary-Tree.md index 8e2cb31..767ea5e 100644 --- a/OJ notes/pages/Leetcode Invert-Binary-Tree.md +++ b/OJ notes/pages/Leetcode Invert-Binary-Tree.md @@ -18,6 +18,7 @@ tag:#recursion tag:#DFS ``` +- [[Leetcode Insert-Into-a-Binary-Search-Tree]] - [[Leetcode Path-Sum]] - [[Leetcode Symmetric-Tree]] diff --git a/OJ notes/pages/Leetcode Maximum-Depth-Of-Binary-Tree.md b/OJ notes/pages/Leetcode Maximum-Depth-Of-Binary-Tree.md index d5ebc7e..7ea62cd 100644 --- a/OJ notes/pages/Leetcode Maximum-Depth-Of-Binary-Tree.md +++ b/OJ notes/pages/Leetcode Maximum-Depth-Of-Binary-Tree.md @@ -18,6 +18,7 @@ tag:#BFS ``` +- [[Breadth First Search]] - [[Leetcode Binary-Tree-Level-Order-Traversal]] - [[Leetcode Search-In-a-Binary-Tree]] - [[Leetcode Symmetric-Tree]] diff --git a/OJ notes/pages/Leetcode Maxinum-subarray.md b/OJ notes/pages/Leetcode Maxinum-subarray.md index 0670967..b2075f3 100644 --- a/OJ notes/pages/Leetcode Maxinum-subarray.md +++ b/OJ notes/pages/Leetcode Maxinum-subarray.md @@ -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]] diff --git a/OJ notes/pages/Leetcode Merge-Sorted-Array.md b/OJ notes/pages/Leetcode Merge-Sorted-Array.md index f7fc273..554880e 100644 --- a/OJ notes/pages/Leetcode Merge-Sorted-Array.md +++ b/OJ notes/pages/Leetcode Merge-Sorted-Array.md @@ -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]] diff --git a/OJ notes/pages/Leetcode Merge-Two-Sorted-Lists.md b/OJ notes/pages/Leetcode Merge-Two-Sorted-Lists.md index 7ae50c0..6429fb8 100644 --- a/OJ notes/pages/Leetcode Merge-Two-Sorted-Lists.md +++ b/OJ notes/pages/Leetcode Merge-Two-Sorted-Lists.md @@ -16,6 +16,7 @@ tag:#two_pointers - [[Leetcode Intersection-of-Two-Arrays-II]] - [[Leetcode Merge-Sorted-Array]] +- [[Two pointers approach]] ##### Links: diff --git a/OJ notes/pages/Leetcode Pascal's-Triangle.md b/OJ notes/pages/Leetcode Pascal's-Triangle.md index 5fd791d..64f4bd4 100644 --- a/OJ notes/pages/Leetcode Pascal's-Triangle.md +++ b/OJ notes/pages/Leetcode Pascal's-Triangle.md @@ -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]] diff --git a/OJ notes/pages/Leetcode Path-Sum.md b/OJ notes/pages/Leetcode Path-Sum.md index bf54d78..f74c2cc 100644 --- a/OJ notes/pages/Leetcode Path-Sum.md +++ b/OJ notes/pages/Leetcode Path-Sum.md @@ -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]] diff --git a/OJ notes/pages/Leetcode Ransom-Note.md b/OJ notes/pages/Leetcode Ransom-Note.md index a6f8694..3b8595a 100644 --- a/OJ notes/pages/Leetcode Ransom-Note.md +++ b/OJ notes/pages/Leetcode Ransom-Note.md @@ -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: diff --git a/OJ notes/pages/Leetcode Reshape-The-Matrix.md b/OJ notes/pages/Leetcode Reshape-The-Matrix.md index befc78b..2f7552c 100644 --- a/OJ notes/pages/Leetcode Reshape-The-Matrix.md +++ b/OJ notes/pages/Leetcode Reshape-The-Matrix.md @@ -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]] diff --git a/OJ notes/pages/Leetcode Search-In-a-Binary-Tree.md b/OJ notes/pages/Leetcode Search-In-a-Binary-Tree.md index db2ef59..0ce2495 100644 --- a/OJ notes/pages/Leetcode Search-In-a-Binary-Tree.md +++ b/OJ notes/pages/Leetcode Search-In-a-Binary-Tree.md @@ -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]] diff --git a/OJ notes/pages/Leetcode Search-Insert-Position.md b/OJ notes/pages/Leetcode Search-Insert-Position.md new file mode 100644 index 0000000..5cc78da --- /dev/null +++ b/OJ notes/pages/Leetcode Search-Insert-Position.md @@ -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& 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; + } +}; +``` \ No newline at end of file diff --git a/OJ notes/pages/Leetcode Search-a-2D-Matrix.md b/OJ notes/pages/Leetcode Search-a-2D-Matrix.md index eda580f..3cf5084 100644 --- a/OJ notes/pages/Leetcode Search-a-2D-Matrix.md +++ b/OJ notes/pages/Leetcode Search-a-2D-Matrix.md @@ -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]] diff --git a/OJ notes/pages/Leetcode Symmetric-Tree.md b/OJ notes/pages/Leetcode Symmetric-Tree.md index c885667..5e57178 100644 --- a/OJ notes/pages/Leetcode Symmetric-Tree.md +++ b/OJ notes/pages/Leetcode Symmetric-Tree.md @@ -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]] diff --git a/OJ notes/pages/Leetcode Two-Sum-IV-Input-Is-a-BST.md b/OJ notes/pages/Leetcode Two-Sum-IV-Input-Is-a-BST.md index afdde98..06070ff 100644 --- a/OJ notes/pages/Leetcode Two-Sum-IV-Input-Is-a-BST.md +++ b/OJ notes/pages/Leetcode Two-Sum-IV-Input-Is-a-BST.md @@ -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]] diff --git a/OJ notes/pages/Leetcode Valid-Parentheses.md b/OJ notes/pages/Leetcode Valid-Parentheses.md similarity index 98% rename from OJ notes/pages/Leetcode Valid-Parentheses.md rename to OJ notes/pages/Leetcode Valid-Parentheses.md index 572907f..92a2c3a 100644 --- a/OJ notes/pages/Leetcode Valid-Parentheses.md +++ b/OJ notes/pages/Leetcode Valid-Parentheses.md @@ -1,4 +1,4 @@ -# Leetcode Valid-Parentheses +# Leetcode Valid-Parentheses #### 2022-06-16 14:50 diff --git a/OJ notes/pages/Leetcode Valid-Sudoku.md b/OJ notes/pages/Leetcode Valid-Sudoku.md index 66a5a4e..5069b92 100644 --- a/OJ notes/pages/Leetcode Valid-Sudoku.md +++ b/OJ notes/pages/Leetcode Valid-Sudoku.md @@ -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: diff --git a/OJ notes/pages/cpp_Range_based_for_loop.md b/OJ notes/pages/cpp_Range_based_for_loop.md index db2a548..cd73ef7 100644 --- a/OJ notes/pages/cpp_Range_based_for_loop.md +++ b/OJ notes/pages/cpp_Range_based_for_loop.md @@ -11,6 +11,8 @@ ___ ```expander tag:#coding_problem tag:#array tag:#vector tag:#multiset -tag:#template_remove_me ``` + + ##### Links: