数组遍历
力扣题目编号,统计数组中的元素 645、697、448、442、41、274
645:错误的集合
集合 S 包含从1到 n 的整数。不幸的是,因为数据错误,导致集合里面某一个元素复制了成了集合里面的另外一个元素的值,导致集合丢失了一个整数并且有一个元素重复。
给定一个数组 nums 代表了集合 S 发生错误后的结果。你的任务是首先寻找到重复出现的整数,再找到丢失的整数,将它们以数组的形式返回。
示例 1:
输入: nums = [1,2,2,4]
输出: [2,3]
注意:
给定数组的长度范围是 [2, 10000]。
给定的数组是无序的。
解题思路
首先通过希尔排序,对无序数组进行排序。得到一个有序数组后,通过标记位的方法,找到重复元素,并标记缺失元素位置。
假如排序后数组为[1,2,2,3,5]
i | val | nums[val - 1] | T | F |
---|---|---|---|---|
0 | 1 | 1 | -1 | |
1 | 2 | 2 | -2 | |
2 | 2 | -2 | 2(重复元素) | |
3 | 3 | 2 | -2 | |
4 | 5 | 5 | -5 |
标记后的数组为[-1,-2,-2,3,-5],即可得当数组元素大于0时,该数组下标加1为缺失元素
实现代码
class Solution {
public:
vector<int> findErrorNums(vector<int>& nums) {
int n = nums.size();
int temp = n/2;
int gap = n/2;
while (gap > 0)
{
for (int i = gap; i < n; i++)
{
temp = nums[i];
int preIndex = i - gap;
while (preIndex >= 0 && nums[preIndex] > temp)
{
nums[preIndex + gap] = nums[preIndex];
preIndex -= gap;
}
nums[preIndex + gap] = temp;
}
gap /= 2;
}
int less = 0, more = 0;
for(int i = 0; i < nums.size(); i++){
int val = abs(nums[i]);
if(nums[val - 1] < 0)
more = val;
else
nums[val - 1] = nums[val - 1]*-1;
}
for(int i = 0; i < nums.size(); i++)
{
cout<<nums[i];
}
for(int i = 0; i < nums.size(); i++){
if(nums[i] > 0)
less = i + 1;
}
return {more, less};
}
};
448:找到所有数组中消失的数字
给定一个范围在 1 ≤ a[i] ≤ n ( n = 数组大小 ) 的 整型数组,数组中的元素一些出现了两次,另一些只出现一次。
找到所有在 [1, n] 范围之间没有出现在数组中的数字。
您能在不使用额外空间且时间复杂度为O(n)的情况下完成这个任务吗? 你可以假定返回的数组不算在额外空间内。
示例:
输入:[4,3,2,7,8,2,3,1]
输出:[5,6]
解题思路
首先观察题目,给定的1 ≤ a[i] ≤ n ( n = 数组大小 )整型数组中,有些出现一次,有些出现多次。
既然a[i]在[1,n]内,那 (a[i]-1) 就在[0,n-1],可以当做数组的索引,对数组的元素进行标记。未被标记元素的索引+1,即为缺失的值。
实现代码
class Solution {
public:
vector<int> findDisappearedNumbers(vector<int>& nums) {
vector<int> res;
if(nums.empty())
return nums;
for(int i = 0 ; i < nums.size() ; i ++)
{
int index = abs(nums[i])-1;
nums[index]=-abs(nums[index]);
}
for(int i = 0 ; i < nums.size() ; i ++)
{
if(nums[i]>0)
res.push_back(i+1);
}
return res;
}
};
442:数组中重复的数据
给定一个整数数组 a,其中1 ≤ a[i] ≤ n (n为数组长度), 其中有些元素出现两次而其他元素出现一次。
找到所有出现两次的元素。
你可以不用到任何额外空间并在O(n)时间复杂度内解决这个问题吗?
示例:
输入:[4,3,2,7,8,2,3,1]
输出:[2,3]
解题思路
跟上一题思路一致,使用数组元素来索引数组。重复数组元素对应的数组值,会在被标记为负数。当重复索引时,直接放到结果集里。
实现代码
class Solution {
public:
vector<int> findDuplicates(vector<int>& nums) {
vector<int> res;
for(int i = 0 ; i< nums.size(); i ++){
int index = abs(nums[i])-1;
if(nums[index]<0){
res.push_back(abs(nums[i]));
}
nums[index] = abs(nums[index]) * (-1);
}
return res;
}
};
274: H 指数
给定一位研究者论文被引用次数的数组(被引用次数是非负整数)。编写一个方法,计算出研究者的 h 指数。
h 指数的定义:h 代表“高引用次数”(high citations),一名科研人员的 h 指数是指他(她)的 (N 篇论文中)总共有 h 篇论文分别被引用了至少 h 次。(其余的 N - h 篇论文每篇被引用次数 不超过 h 次。)
例如:某人的 h 指数是 20,这表示他已发表的论文中,每篇被引用了至少 20 次的论文总共有 20 篇。
示例:
输入:citations = [3,0,6,1,5]
输出:3
解释:给定数组表示研究者总共有 5 篇论文,每篇论文相应的被引用了 3, 0, 6, 1, 5 次。
由于研究者有 3 篇论文每篇 至少 被引用了 3 次,其余两篇论文每篇被引用 不多于 3 次,所以她的 h 指数是 3。
提示:如果 h 有多种可能的值,h 指数是其中最大的那个。
解题思路
首先通过希尔排序,对无序数组进行排序。得到一个有序数组后,从后往前遍历,判断 citations [ j ] > = (n-j) ,如果为true则把当前H指数赋值为 n-j ,为false,则表示res是最大H指数。
实现代码
class Solution {
public:
int hIndex(vector<int>& citations) {
int n = citations.size();
int res = 0;
int temp = n / 2;
int gap = n / 2;
while (gap > 0)
{
for (int i = gap; i < n; i++)
{
temp = citations[i];
int preIndex = i - gap;
while (preIndex >= 0 && citations[preIndex] > temp)
{
citations[preIndex + gap] = citations[preIndex];
preIndex -= gap;
}
citations[preIndex + gap] = temp;
}
gap /= 2;
}
for(int j = n-1 ; j>=0 ; j --){
if(citations[j] >= (n-j)){
if(n-j>res){
res = n-j;
}
}
}
return res;
}
};