From 2d926ff3cb608e5e6e8d969f0ab70c270bcbab31 Mon Sep 17 00:00:00 2001 From: Morisummer Date: Thu, 15 Jul 2021 23:14:17 +0800 Subject: [PATCH] =?UTF-8?q?=E6=95=B0=E7=BB=84=E7=AF=87=E5=A2=9E=E5=8A=A0?= =?UTF-8?q?=E9=83=A8=E5=88=86C++=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../数组篇/leetcode219数组中重复元素2.md | 36 +++++++++- .../数组篇/leetcode54螺旋矩阵.md | 36 ++++++++++ .../数组篇/leetcode59螺旋矩阵2.md | 72 +++++++++++++++++++ animation-simulation/数组篇/leetcode66加一.md | 20 +++++- .../数组篇/leetcode75颜色分类.md | 47 +++++++++++- 5 files changed, 208 insertions(+), 3 deletions(-) diff --git a/animation-simulation/数组篇/leetcode219数组中重复元素2.md b/animation-simulation/数组篇/leetcode219数组中重复元素2.md index d5b5d9d..f8f2edc 100644 --- a/animation-simulation/数组篇/leetcode219数组中重复元素2.md +++ b/animation-simulation/数组篇/leetcode219数组中重复元素2.md @@ -78,6 +78,22 @@ class Solution: return False ``` +C++ Code: + +```cpp +class Solution { +public: + bool containsNearbyDuplicate(vector& nums, int k) { + unordered_map m; + for(int i = 0; i < nums.size(); ++i){ + if(m.count(nums[i]) && i - m[nums[i]] <= k) return true; + m[nums[i]] = i; + } + return false; + } +}; +``` + **HashSet** **解析** @@ -139,4 +155,22 @@ class Solution: if len(s) > k: s.remove(nums[i - k]) return False -``` +``` + +C++ Code: + +```cpp +class Solution { +public: + bool containsNearbyDuplicate(vector& nums, int k) { + multiset S; + for(int i = 0; i < nums.size(); ++i){ + if(S.count(nums[i])) return true; + S.insert(nums[i]); + if(S.size() > k) S.erase(nums[i - k]); + } + return false; + } +}; +``` + diff --git a/animation-simulation/数组篇/leetcode54螺旋矩阵.md b/animation-simulation/数组篇/leetcode54螺旋矩阵.md index cb3c678..539c672 100644 --- a/animation-simulation/数组篇/leetcode54螺旋矩阵.md +++ b/animation-simulation/数组篇/leetcode54螺旋矩阵.md @@ -85,6 +85,42 @@ class Solution { ``` +C++ Code: + +```cpp +class Solution { +public: + vector spiralOrder(vector>& matrix) { + vector arr; + int left = 0, right = matrix[0].size()-1; + int top = 0, down = matrix.size()-1; + while (true) { + for (int i = left; i <= right; ++i) { + arr.emplace_back(matrix[top][i]); + } + top++; + if (top > down) break; + for (int i = top; i <= down; ++i) { + arr.emplace_back(matrix[i][right]); + } + right--; + if (left > right) break; + for (int i = right; i >= left; --i) { + arr.emplace_back(matrix[down][i]); + } + down--; + if (top > down) break; + for (int i = down; i >= top; --i) { + arr.emplace_back(matrix[i][left]); + } + left++; + if (left > right) break; + } + return arr; + } +}; +``` + Python3 Code: ```python diff --git a/animation-simulation/数组篇/leetcode59螺旋矩阵2.md b/animation-simulation/数组篇/leetcode59螺旋矩阵2.md index 50cf94c..543aab3 100644 --- a/animation-simulation/数组篇/leetcode59螺旋矩阵2.md +++ b/animation-simulation/数组篇/leetcode59螺旋矩阵2.md @@ -140,6 +140,42 @@ class Solution: return arr ``` +C++ Code: + +```cpp +class Solution { +public: + vector spiralOrder(vector>& matrix) { + vector arr; + int left = 0, right = matrix[0].size()-1; + int top = 0, down = matrix.size()-1; + while (true) { + for (int i = left; i <= right; ++i) { + arr.emplace_back(matrix[top][i]); + } + top++; + if (top > down) break; + for (int i = top; i <= down; ++i) { + arr.emplace_back(matrix[i][right]); + } + right--; + if (left > right) break; + for (int i = right; i >= left; --i) { + arr.emplace_back(matrix[down][i]); + } + down--; + if (top > down) break; + for (int i = down; i >= top; --i) { + arr.emplace_back(matrix[i][left]); + } + left++; + if (left > right) break; + } + return arr; + } +}; +``` + 我们仅仅是将 54 反过来了,往螺旋矩阵里面插值,下面我们直接看代码吧,大家可以也可以对其改进,大家可以思考一下,如果修改能够让代码更简洁! Java Code: @@ -226,3 +262,39 @@ class Solution: return arr.tolist() ``` +C++ Code: + +```cpp +class Solution { +public: + vector> generateMatrix(int n) { + vector > arr(n, vector (n)); + int left = 0, right = n-1, top = 0, buttom = n - 1, num = 1, numsize = n * n; + while (true) { + for (int i = left; i <= right; ++i) { + arr[top][i] = num++; + } + top++; + if (num > numsize) break; + for (int i = top; i <= buttom; ++i) { + arr[i][right] = num++; + } + right--; + if (num > numsize) break; + for (int i = right; i >= left; --i) { + arr[buttom][i] = num++; + } + buttom--; + if (num > numsize) break; + for (int i = buttom; i >= top; --i) { + arr[i][left] = num++; + } + left++; + if (num > numsize) break; + + } + return arr; + } +}; +``` + diff --git a/animation-simulation/数组篇/leetcode66加一.md b/animation-simulation/数组篇/leetcode66加一.md index 3030fee..9b941c5 100644 --- a/animation-simulation/数组篇/leetcode66加一.md +++ b/animation-simulation/数组篇/leetcode66加一.md @@ -3,7 +3,6 @@ > 感谢支持,该仓库会一直维护,希望对各位有一丢丢帮助。 > > 另外希望手机阅读的同学可以来我的 [**公众号:袁厨的算法小屋**](https://raw.githubusercontent.com/tan45du/test/master/微信图片_20210320152235.2pthdebvh1c0.png) 两个平台同步,想要和题友一起刷题,互相监督的同学,可以在我的小屋点击[**刷题小队**](https://raw.githubusercontent.com/tan45du/test/master/微信图片_20210320152235.2pthdebvh1c0.png)进入。 -> #### [66. 加一](https://leetcode-cn.com/problems/plus-one/) @@ -85,3 +84,22 @@ class Solution: arr[0] = 1 return arr ``` + +C++ Code: + +```cpp +class Solution { +public: + vector plusOne(vector& digits) { + for(int i = digits.size() - 1; i >= 0; --i){ + digits[i] = (digits[i] + 1)%10; + if(digits[i]) return digits; + } + for(int & x: digits) x = 0; + digits.emplace_back(1); + reverse(digits.begin(), digits.end()); + return digits; + } +}; +``` + diff --git a/animation-simulation/数组篇/leetcode75颜色分类.md b/animation-simulation/数组篇/leetcode75颜色分类.md index 7f125f1..f0f497e 100644 --- a/animation-simulation/数组篇/leetcode75颜色分类.md +++ b/animation-simulation/数组篇/leetcode75颜色分类.md @@ -96,6 +96,27 @@ class Solution: nums[j] = temp ``` +C++ Code: + +```cpp +class Solution { +public: + void sortColors(vector& nums) { + int len = nums.size(), left = 0; + int i = left, right = len-1; + while (i <= right) { + if (nums[i] == 2) { + swap(nums[i],nums[right--]); + } else if (nums[i] == 0) { + swap(nums[i++],nums[left++]); + } else { + i++; + } + } + } +}; +``` + 另外我们看这段代码,有什么问题呢?那就是我们即使完全符合时,仍会交换元素,这样会大大降低我们的效率。 例如:[0,0,0,1,1,1,2,2,2] @@ -174,4 +195,28 @@ class Solution: temp = nums[i] nums[i] = nums[j] nums[j] = temp -``` \ No newline at end of file +``` + +C++ Code: + +```cpp +class Solution { +public: + void sortColors(vector& nums) { + int left = 0, len = nums.size(); + int right = len - 1; + for (int i = 0; i <= right; ++i) { + if (nums[i] == 0) { + swap(nums[i],nums[left++]); + } + if (nums[i] == 2) { + swap(nums[i],nums[right--]); + if (nums[i] != 1) { + i--; + } + } + } + } +}; +``` +