From 3d64f4442981c5e9dbd6e13d1b1fb7fef9134fff Mon Sep 17 00:00:00 2001 From: juan Date: Sun, 10 Jul 2022 10:26:30 +0800 Subject: [PATCH] vault backup: 2022-07-10 10:26:30 --- OJ notes/OJ-index.md | 12 ++++++++-- .../Leetcode Intersection-of-Two-Arrays-II.md | 1 + OJ notes/pages/Leetcode Merge-Sorted-Array.md | 1 + .../pages/Leetcode Merge-Two-Sorted-Lists.md | 1 + OJ notes/pages/Leetcode Pascal's-Triangle.md | 2 ++ OJ notes/pages/Leetcode Reshape-The-Matrix.md | 2 ++ OJ notes/pages/Leetcode Rotate-Array.md | 23 +++++++++++++++++++ OJ notes/pages/Two pointers approach.md | 1 + 8 files changed, 41 insertions(+), 2 deletions(-) diff --git a/OJ notes/OJ-index.md b/OJ notes/OJ-index.md index 8b91ebf..3b65dc4 100644 --- a/OJ notes/OJ-index.md +++ b/OJ notes/OJ-index.md @@ -23,6 +23,7 @@ tag:#CS_list_need_understanding - [ ] [[Leetcode Binary-Tree-Preorder-Traversal]] - [ ] [[Leetcode Implement-Queue-Using-Stacks]] - [ ] [[Leetcode Reverse-Linked-List]] +- [ ] [[Leetcode Rotate-Array]] @@ -35,6 +36,7 @@ tag:#CS_list_need_practicing - [ ] [[Leetcode Binary-Search]] - [ ] [[Leetcode Binary-Tree-Postorder-Traversal]] - [ ] [[Leetcode Reverse-Linked-List]] +- [ ] [[Leetcode Rotate-Array]] - [ ] [[Leetcode Validate-Binary-Search-Tree]] @@ -48,7 +50,6 @@ tag:#leetcode - [[$filename]] ``` -- [[Leetcode Valid-Parentheses]] - [[Leetcode Best-Time-To-Buy-And-Sell-Stock]] - [[Leetcode Binary-Search]] - [[Leetcode Binary-Tree-Inorder-Traversal]] @@ -75,13 +76,16 @@ tag:#leetcode - [[Leetcode Remove-Linked-List-Elements]] - [[Leetcode Reshape-The-Matrix]] - [[Leetcode Reverse-Linked-List]] +- [[Leetcode Rotate-Array]] - [[Leetcode Search-a-2D-Matrix]] - [[Leetcode Search-In-a-Binary-Tree]] - [[Leetcode Search-Insert-Position]] +- [[Leetcode Squares-of-a-Sorted-Array]] - [[Leetcode Symmetric-Tree]] - [[Leetcode Two-Sum]] - [[Leetcode Two-Sum-IV-Input-Is-a-BST]] - [[Leetcode Valid-Anagram]] +- [[Leetcode Valid-Parentheses]] - [[Leetcode Valid-Sudoku]] - [[Leetcode Validate-Binary-Search-Tree]] @@ -107,7 +111,6 @@ tag:#DS tag:#coding_problem -tag:#template_remove_me - [[$filename]] ``` -- [[Leetcode Valid-Parentheses]] - [[Leetcode Best-Time-To-Buy-And-Sell-Stock]] - [[Leetcode Binary-Search]] - [[Leetcode Binary-Tree-Inorder-Traversal]] @@ -133,13 +136,16 @@ tag:#DS tag:#coding_problem -tag:#template_remove_me - [[Leetcode Remove-Linked-List-Elements]] - [[Leetcode Reshape-The-Matrix]] - [[Leetcode Reverse-Linked-List]] +- [[Leetcode Rotate-Array]] - [[Leetcode Search-a-2D-Matrix]] - [[Leetcode Search-In-a-Binary-Tree]] - [[Leetcode Search-Insert-Position]] +- [[Leetcode Squares-of-a-Sorted-Array]] - [[Leetcode Symmetric-Tree]] - [[Leetcode Two-Sum]] - [[Leetcode Two-Sum-IV-Input-Is-a-BST]] - [[Leetcode Valid-Anagram]] +- [[Leetcode Valid-Parentheses]] - [[Leetcode Valid-Sudoku]] - [[Leetcode Validate-Binary-Search-Tree]] @@ -188,9 +194,11 @@ tag:#algorithm tag:#coding_problem -tag:#template_remove_me - [[Leetcode Path-Sum]] - [[Leetcode Ransom-Note]] - [[Leetcode Reverse-Linked-List]] +- [[Leetcode Rotate-Array]] - [[Leetcode Search-a-2D-Matrix]] - [[Leetcode Search-In-a-Binary-Tree]] - [[Leetcode Search-Insert-Position]] +- [[Leetcode Squares-of-a-Sorted-Array]] - [[Leetcode Symmetric-Tree]] - [[Leetcode Two-Sum]] - [[Leetcode Two-Sum-IV-Input-Is-a-BST]] 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 ab24631..c06e51d 100644 --- a/OJ notes/pages/Leetcode Intersection-of-Two-Arrays-II.md +++ b/OJ notes/pages/Leetcode Intersection-of-Two-Arrays-II.md @@ -19,6 +19,7 @@ tag:#unordered_map OR tag:#two_pointers OR tag:#sort - [[cpp_std_unordered_map]] - [[Leetcode Merge-Sorted-Array]] - [[Leetcode Merge-Two-Sorted-Lists]] +- [[Leetcode Squares-of-a-Sorted-Array]] - [[Leetcode Two-Sum]] - [[Two pointers approach]] diff --git a/OJ notes/pages/Leetcode Merge-Sorted-Array.md b/OJ notes/pages/Leetcode Merge-Sorted-Array.md index 554880e..803cc87 100644 --- a/OJ notes/pages/Leetcode Merge-Sorted-Array.md +++ b/OJ notes/pages/Leetcode Merge-Sorted-Array.md @@ -21,6 +21,7 @@ tag:#set OR tag:#multiset OR tag:#merge_sort OR tag:#vector OR tag:#two_pointers - [[Leetcode Merge-Two-Sorted-Lists]] - [[Leetcode Reshape-The-Matrix]] - [[Leetcode Search-a-2D-Matrix]] +- [[Leetcode Squares-of-a-Sorted-Array]] - [[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 6429fb8..290b0a4 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]] +- [[Leetcode Squares-of-a-Sorted-Array]] - [[Two pointers approach]] diff --git a/OJ notes/pages/Leetcode Pascal's-Triangle.md b/OJ notes/pages/Leetcode Pascal's-Triangle.md index 64f4bd4..303dd0c 100644 --- a/OJ notes/pages/Leetcode Pascal's-Triangle.md +++ b/OJ notes/pages/Leetcode Pascal's-Triangle.md @@ -25,7 +25,9 @@ tag:#recursion OR tag:#iteration OR tag:#array -tag:#Kadane_s_algorithm - [[Leetcode Ransom-Note]] - [[Leetcode Reshape-The-Matrix]] - [[Leetcode Reverse-Linked-List]] +- [[Leetcode Rotate-Array]] - [[Leetcode Search-Insert-Position]] +- [[Leetcode Squares-of-a-Sorted-Array]] - [[Leetcode Symmetric-Tree]] - [[Leetcode Two-Sum]] - [[Leetcode Valid-Anagram]] diff --git a/OJ notes/pages/Leetcode Reshape-The-Matrix.md b/OJ notes/pages/Leetcode Reshape-The-Matrix.md index 2f7552c..c41e3ab 100644 --- a/OJ notes/pages/Leetcode Reshape-The-Matrix.md +++ b/OJ notes/pages/Leetcode Reshape-The-Matrix.md @@ -20,8 +20,10 @@ - [[Leetcode Merge-Sorted-Array]] - [[Leetcode Pascal's-Triangle]] - [[Leetcode Ransom-Note]] +- [[Leetcode Rotate-Array]] - [[Leetcode Search-a-2D-Matrix]] - [[Leetcode Search-Insert-Position]] +- [[Leetcode Squares-of-a-Sorted-Array]] - [[Leetcode Two-Sum]] - [[Leetcode Valid-Anagram]] - [[Leetcode Valid-Sudoku]] diff --git a/OJ notes/pages/Leetcode Rotate-Array.md b/OJ notes/pages/Leetcode Rotate-Array.md index ff9f3a0..f1f5a91 100644 --- a/OJ notes/pages/Leetcode Rotate-Array.md +++ b/OJ notes/pages/Leetcode Rotate-Array.md @@ -96,7 +96,30 @@ Remember to sanitize k, when k > size Method 1: ```cpp +class Solution { + void swap(vector &nums, int l, int r) { + int temp = nums[l]; + nums[l] = nums[r]; + nums[r] = temp; + } + void reverse(vector &nums, int l, int r) { + while (l < r) { + swap(nums, l, r); + l++; + r--; + } + } +public: + void rotate(vector &nums, int k) { + const int size = nums.size(); + k = k % size; + // Reversion + reverse(nums, 0, size - k - 1); + reverse(nums, size - k, size - 1); + reverse(nums, 0, size - 1); + } +}; ``` Method 2: diff --git a/OJ notes/pages/Two pointers approach.md b/OJ notes/pages/Two pointers approach.md index 75f8732..6621332 100644 --- a/OJ notes/pages/Two pointers approach.md +++ b/OJ notes/pages/Two pointers approach.md @@ -17,6 +17,7 @@ tag:#coding_problem tag:#two_pointers -tag:#template_remove_me - [[Leetcode Intersection-of-Two-Arrays-II]] - [[Leetcode Merge-Sorted-Array]] - [[Leetcode Merge-Two-Sorted-Lists]] +- [[Leetcode Squares-of-a-Sorted-Array]] ##### Links: