数组篇增加部分C++代码

This commit is contained in:
Morisummer 2021-07-15 23:14:17 +08:00
parent 2c4afffbd3
commit 2d926ff3cb
5 changed files with 208 additions and 3 deletions

View File

@ -78,6 +78,22 @@ class Solution:
return False return False
``` ```
C++ Code:
```cpp
class Solution {
public:
bool containsNearbyDuplicate(vector<int>& nums, int k) {
unordered_map <int, int> 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** **HashSet**
**解析** **解析**
@ -140,3 +156,21 @@ class Solution:
s.remove(nums[i - k]) s.remove(nums[i - k])
return False return False
``` ```
C++ Code:
```cpp
class Solution {
public:
bool containsNearbyDuplicate(vector<int>& nums, int k) {
multiset <int> 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;
}
};
```

View File

@ -85,6 +85,42 @@ class Solution {
``` ```
C++ Code:
```cpp
class Solution {
public:
vector<int> spiralOrder(vector<vector<int>>& matrix) {
vector <int> 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: Python3 Code:
```python ```python

View File

@ -140,6 +140,42 @@ class Solution:
return arr return arr
``` ```
C++ Code:
```cpp
class Solution {
public:
vector<int> spiralOrder(vector<vector<int>>& matrix) {
vector <int> 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 反过来了往螺旋矩阵里面插值下面我们直接看代码吧,大家可以也可以对其改进大家可以思考一下如果修改能够让代码更简洁 我们仅仅是将 54 反过来了往螺旋矩阵里面插值下面我们直接看代码吧,大家可以也可以对其改进大家可以思考一下如果修改能够让代码更简洁
Java Code: Java Code:
@ -226,3 +262,39 @@ class Solution:
return arr.tolist() return arr.tolist()
``` ```
C++ Code:
```cpp
class Solution {
public:
vector<vector<int>> generateMatrix(int n) {
vector <vector <int>> arr(n, vector <int>(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;
}
};
```

View File

@ -3,7 +3,6 @@
> 感谢支持该仓库会一直维护希望对各位有一丢丢帮助 > 感谢支持该仓库会一直维护希望对各位有一丢丢帮助
> >
> 另外希望手机阅读的同学可以来我的 <u>[**公众号袁厨的算法小屋**](https://raw.githubusercontent.com/tan45du/test/master/微信图片_20210320152235.2pthdebvh1c0.png)</u> 两个平台同步想要和题友一起刷题互相监督的同学可以在我的小屋点击<u>[**刷题小队**](https://raw.githubusercontent.com/tan45du/test/master/微信图片_20210320152235.2pthdebvh1c0.png)</u>进入 > 另外希望手机阅读的同学可以来我的 <u>[**公众号袁厨的算法小屋**](https://raw.githubusercontent.com/tan45du/test/master/微信图片_20210320152235.2pthdebvh1c0.png)</u> 两个平台同步想要和题友一起刷题互相监督的同学可以在我的小屋点击<u>[**刷题小队**](https://raw.githubusercontent.com/tan45du/test/master/微信图片_20210320152235.2pthdebvh1c0.png)</u>进入
>
#### [66. 加一](https://leetcode-cn.com/problems/plus-one/) #### [66. 加一](https://leetcode-cn.com/problems/plus-one/)
@ -85,3 +84,22 @@ class Solution:
arr[0] = 1 arr[0] = 1
return arr return arr
``` ```
C++ Code:
```cpp
class Solution {
public:
vector<int> plusOne(vector<int>& 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;
}
};
```

View File

@ -96,6 +96,27 @@ class Solution:
nums[j] = temp nums[j] = temp
``` ```
C++ Code:
```cpp
class Solution {
public:
void sortColors(vector<int>& 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] 例如[0,0,0,1,1,1,2,2,2]
@ -175,3 +196,27 @@ class Solution:
nums[i] = nums[j] nums[i] = nums[j]
nums[j] = temp nums[j] = temp
``` ```
C++ Code:
```cpp
class Solution {
public:
void sortColors(vector<int>& 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--;
}
}
}
}
};
```