From 19b8ca6a04c6b63017f2402d969b66f66726b0c9 Mon Sep 17 00:00:00 2001 From: 3119005212 <1136153224@qq.com> Date: Thu, 20 May 2021 17:17:30 +0800 Subject: [PATCH] =?UTF-8?q?=E6=95=B0=E7=BB=84=E7=AF=87=E6=9B=B4=E6=96=B0?= =?UTF-8?q?=E5=89=91=E6=8C=87offer=EF=BC=8Clc209=EF=BC=8C=20lc001=EF=BC=8C?= =?UTF-8?q?lc27=E7=9A=84cpp=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../数组篇/leetcode27移除元素.md | 22 +++++++++++++++++-- .../数组篇/leetcode41缺失的第一个正数.md | 2 +- .../数组篇/剑指offer3数组中重复的数.md | 21 ++++++++++++++++++ .../数组篇/长度最小的子数组.md | 22 +++++++++++++++++++ 4 files changed, 64 insertions(+), 3 deletions(-) diff --git a/animation-simulation/数组篇/leetcode27移除元素.md b/animation-simulation/数组篇/leetcode27移除元素.md index a7a51b8..1a14c5b 100644 --- a/animation-simulation/数组篇/leetcode27移除元素.md +++ b/animation-simulation/数组篇/leetcode27移除元素.md @@ -95,7 +95,7 @@ class Solution { return 0; } int i = 0; - for (int j = 0; j < nums.length; ++j) { + for (int j = 0; j < len; ++j) { //如果等于目标值,则删除 if (nums[j] == val) { continue; @@ -110,7 +110,7 @@ class Solution { Python3 Code: -```py +```python class Solution: def removeElement(self, nums: List[int], val: int) -> int: i = 0 @@ -121,3 +121,21 @@ class Solution: return i ``` +C++ Code: + +```cpp +class Solution { +public: + int removeElement(vector& nums, int val) { + int n = nums.size(); + if(!n) return 0; + int i = 0; + for(int j = 0; j < n; ++j){ + if(nums[j] == val) continue; + nums[i++] = nums[j]; + } + return i; + } +}; +``` + diff --git a/animation-simulation/数组篇/leetcode41缺失的第一个正数.md b/animation-simulation/数组篇/leetcode41缺失的第一个正数.md index 6c46f4d..73dacaf 100644 --- a/animation-simulation/数组篇/leetcode41缺失的第一个正数.md +++ b/animation-simulation/数组篇/leetcode41缺失的第一个正数.md @@ -119,7 +119,7 @@ class Solution { Python3 Code: -```py +```python class Solution: def firstMissingPositive(self, nums: List[int]) -> int: n = len(nums) diff --git a/animation-simulation/数组篇/剑指offer3数组中重复的数.md b/animation-simulation/数组篇/剑指offer3数组中重复的数.md index f20a2d0..e29cc93 100644 --- a/animation-simulation/数组篇/剑指offer3数组中重复的数.md +++ b/animation-simulation/数组篇/剑指offer3数组中重复的数.md @@ -57,6 +57,8 @@ class Solution { **题目代码** +Java Code: + ```java class Solution { public int findRepeatNumber(int[] nums) { @@ -80,3 +82,22 @@ class Solution { } ``` +C++ Code: + +```cpp +class Solution { +public: + int findRepeatNumber(vector& nums) { + if(nums.empty()) return 0; + int n = nums.size(); + for(int i = 0; i < n; ++i){ + while(nums[i] != i){ + if(nums[i] == nums[nums[i]]) return nums[i]; + swap(nums[i], nums[nums[i]]); + } + } + return -1; + } +}; +``` + diff --git a/animation-simulation/数组篇/长度最小的子数组.md b/animation-simulation/数组篇/长度最小的子数组.md index d0eaee6..2e88fb0 100644 --- a/animation-simulation/数组篇/长度最小的子数组.md +++ b/animation-simulation/数组篇/长度最小的子数组.md @@ -38,6 +38,8 @@ #### 题目代码 +Java Code: + ```java class Solution { public int minSubArrayLen(int s, int[] nums) { @@ -60,3 +62,23 @@ class Solution { } ``` +C++ Code: + +```cpp +class Solution { +public: + int minSubArrayLen(int t, vector& nums) { + int n = nums.size(); + int i = 0, sum = 0, winlen = INT_MAX; + for(int j = 0; j < n; ++j){ + sum += nums[j]; + while(sum >= t){ + winlen = min(winlen, j - i + 1); + sum -= nums[i++]; + } + } + return winlen == INT_MAX? 0: winlen; + } +}; +``` +