数组篇更新剑指offer,lc209, lc001,lc27的cpp代码

This commit is contained in:
3119005212 2021-05-20 17:17:30 +08:00
parent 696c5bdfd1
commit 19b8ca6a04
4 changed files with 64 additions and 3 deletions

View File

@ -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<int>& 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;
}
};
```

View File

@ -119,7 +119,7 @@ class Solution {
Python3 Code:
```py
```python
class Solution:
def firstMissingPositive(self, nums: List[int]) -> int:
n = len(nums)

View File

@ -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<int>& 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;
}
};
```

View File

@ -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<int>& 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;
}
};
```