mirror of
https://github.com/chefyuan/algorithm-base.git
synced 2026-03-12 12:51:10 +00:00
[update] #3 数组篇添加C++ Code
This commit is contained in:
@@ -2,7 +2,7 @@
|
||||
>
|
||||
> 感谢支持,该仓库会一直维护,希望对各位有一丢丢帮助。
|
||||
>
|
||||
> 另外希望手机阅读的同学可以来我的 <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>进入。
|
||||
|
||||
#### [41. 缺失的第一个正数](https://leetcode-cn.com/problems/first-missing-positive/)
|
||||
|
||||
@@ -53,15 +53,15 @@ class Solution {
|
||||
for (int x : nums) {
|
||||
if (x > 0 && x < res.length) {
|
||||
res[x] = x;
|
||||
}
|
||||
}
|
||||
}
|
||||
//遍历查找,发现不一样时直接返回
|
||||
for (int i = 1; i < res.length; i++) {
|
||||
if (res[i] != i) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
}
|
||||
//缺少最后一个,例如 1,2,3此时缺少 4 ,细节2
|
||||
//缺少最后一个,例如 1,2,3此时缺少 4 ,细节2
|
||||
return res.length;
|
||||
}
|
||||
}
|
||||
@@ -85,7 +85,7 @@ class Solution:
|
||||
for i in range(1, len(res)):
|
||||
if res[i] != i:
|
||||
return i
|
||||
# 缺少最后一个,例如 1,2,3此时缺少 4 ,细节2
|
||||
# 缺少最后一个,例如 1,2,3此时缺少 4 ,细节2
|
||||
return len(res)
|
||||
```
|
||||
|
||||
@@ -145,7 +145,7 @@ class Solution {
|
||||
for (int i = 0; i < len; ++i) {
|
||||
//需要考虑指针移动情况,大于0,小于len+1,不等与i+1,两个交换的数相等时,防止死循环
|
||||
while (nums[i] > 0 && nums[i] < len + 1 && nums[i] != i+1 && nums[i] != nums[nums[i]-1]) {
|
||||
swap(nums,i,nums[i] - 1);
|
||||
swap(nums,i,nums[i] - 1);
|
||||
}
|
||||
}
|
||||
//遍历寻找缺失的正整数
|
||||
@@ -207,10 +207,10 @@ class Solution {
|
||||
for i in 0..<len {
|
||||
// 需要考虑指针移动情况,大于0,小于len+1,不等与i+1,
|
||||
// 两个交换的数相等时,防止死循环
|
||||
while nums[i] > 0
|
||||
&& nums[i] < len + 1
|
||||
while nums[i] > 0
|
||||
&& nums[i] < len + 1
|
||||
&& nums[i] != i + 1
|
||||
&& nums[i] != nums[nums[i] - 1]
|
||||
&& nums[i] != nums[nums[i] - 1]
|
||||
{
|
||||
//nums.swapAt(i, (nums[i] - 1)) // 系统方法
|
||||
self.swap(&nums, i, (nums[i] - 1)) // 自定义方法
|
||||
@@ -232,3 +232,44 @@ class Solution {
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
C++ Code
|
||||
|
||||
```C++
|
||||
class Solution
|
||||
{
|
||||
public:
|
||||
int firstMissingPositive(vector<int> &nums)
|
||||
{
|
||||
int size = nums.size();
|
||||
//判断范围是否符合要求
|
||||
auto inRange = [](auto s, auto e)
|
||||
{
|
||||
return [s, e](auto &n)
|
||||
{
|
||||
return e >= n && n >= s;
|
||||
};
|
||||
};
|
||||
auto cusInRange = inRange(1, size);
|
||||
//增加数组长度, 便于计算, 不需要再转换
|
||||
nums.push_back(0);
|
||||
|
||||
for (int i = 0; i < size; i++)
|
||||
{
|
||||
//将不在正确位置的元素放到正确位置上
|
||||
while (cusInRange(nums[i]) && nums[i] != i && nums[nums[i]] != nums[i])
|
||||
{
|
||||
swap(nums[i], nums[nums[i]]);
|
||||
}
|
||||
}
|
||||
|
||||
//找出缺失的元素
|
||||
for (int i = 1; i <= size; i++)
|
||||
{
|
||||
if (nums[i] != i)
|
||||
return i;
|
||||
}
|
||||
return size + 1;
|
||||
}
|
||||
};
|
||||
```
|
||||
|
||||
Reference in New Issue
Block a user