Skip to content

Commit 42ecabd

Browse files
committed
2 parents 013f3f0 + 697db85 commit 42ecabd

File tree

4 files changed

+64
-3
lines changed

4 files changed

+64
-3
lines changed

animation-simulation/数组篇/leetcode27移除元素.md

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ class Solution {
9595
return 0;
9696
}
9797
int i = 0;
98-
for (int j = 0; j < nums.length; ++j) {
98+
for (int j = 0; j < len; ++j) {
9999
//如果等于目标值,则删除
100100
if (nums[j] == val) {
101101
continue;
@@ -110,7 +110,7 @@ class Solution {
110110

111111
Python3 Code:
112112

113-
```py
113+
```python
114114
class Solution:
115115
def removeElement(self, nums: List[int], val: int) -> int:
116116
i = 0
@@ -121,3 +121,21 @@ class Solution:
121121
return i
122122
```
123123

124+
C++ Code:
125+
126+
```cpp
127+
class Solution {
128+
public:
129+
int removeElement(vector<int>& nums, int val) {
130+
int n = nums.size();
131+
if(!n) return 0;
132+
int i = 0;
133+
for(int j = 0; j < n; ++j){
134+
if(nums[j] == val) continue;
135+
nums[i++] = nums[j];
136+
}
137+
return i;
138+
}
139+
};
140+
```
141+

animation-simulation/数组篇/leetcode41缺失的第一个正数.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ class Solution {
119119

120120
Python3 Code:
121121

122-
```py
122+
```python
123123
class Solution:
124124
def firstMissingPositive(self, nums: List[int]) -> int:
125125
n = len(nums)

animation-simulation/数组篇/剑指offer3数组中重复的数.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ class Solution {
5757

5858
**题目代码**
5959

60+
Java Code:
61+
6062
```java
6163
class Solution {
6264
public int findRepeatNumber(int[] nums) {
@@ -80,3 +82,22 @@ class Solution {
8082
}
8183
```
8284

85+
C++ Code:
86+
87+
```cpp
88+
class Solution {
89+
public:
90+
int findRepeatNumber(vector<int>& nums) {
91+
if(nums.empty()) return 0;
92+
int n = nums.size();
93+
for(int i = 0; i < n; ++i){
94+
while(nums[i] != i){
95+
if(nums[i] == nums[nums[i]]) return nums[i];
96+
swap(nums[i], nums[nums[i]]);
97+
}
98+
}
99+
return -1;
100+
}
101+
};
102+
```
103+

animation-simulation/数组篇/长度最小的子数组.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@
3838

3939
#### 题目代码
4040

41+
Java Code:
42+
4143
```java
4244
class Solution {
4345
public int minSubArrayLen(int s, int[] nums) {
@@ -60,3 +62,23 @@ class Solution {
6062
}
6163
```
6264

65+
C++ Code:
66+
67+
```cpp
68+
class Solution {
69+
public:
70+
int minSubArrayLen(int t, vector<int>& nums) {
71+
int n = nums.size();
72+
int i = 0, sum = 0, winlen = INT_MAX;
73+
for(int j = 0; j < n; ++j){
74+
sum += nums[j];
75+
while(sum >= t){
76+
winlen = min(winlen, j - i + 1);
77+
sum -= nums[i++];
78+
}
79+
}
80+
return winlen == INT_MAX? 0: winlen;
81+
}
82+
};
83+
```
84+

0 commit comments

Comments
 (0)