查找第一个数组中不存在的元素
Find elements which are present in the first array and not in the second
如何从给定的两个数组中找到第二个数组中不存在的数字C++?
例如:
输入:
10 20 30 40 50 60 70 80 90 100
10 20 30 40 50 50 40 30 20 10
输出:
60 70 80 90 100
<小时 />输入:
1 2 3 4 5 6 7 8 9 10
-1 -2 -3 -4 -5 -6 -7 -8 -9 -10
输出:
1 2 3 4 5 6 7 8 9 10
我的代码:
#include <iostream>
int main()
{
const int n = 10;
int arr1[n];
const int m = 10;
int arr2[n];
int uncommon = 0;
for (int i = 0; i < n; ++i)
{
std::cin >> arr1[i];
}
for (int j = 0; j < m; ++j)
{
std::cin >> arr2[j];
}
for (int i = 0; i < n; ++i)
{
count = 0;
for (int j = 0; j < m; ++j)
{
if (arr1[i] == arr2[j])
uncommon++;
}
if (uncommon == 0)
std::cout << arr1[i];
}
}
std::sort 和 std::set_difference 的使用可用于输出两个数组中的差值:
#include <iostream>
#include <algorithm>
#include <iterator>
int main()
{
int a[] = { 10, 20, 30, 40, 50, 60, 70, 80, 90, 100 };
int b[] = { 10, 20, 30, 40, 50, 50, 40, 30, 20, 10 };
// sort the two arrays
std::sort(std::begin(a), std::end(a));
std::sort(std::begin(b), std::end(b));
// get the set_difference and output the results
std::set_difference(std::begin(a), std::end(a),
std::begin(b), std::end(b), std::ostream_iterator<int>(std::cout, " "));
}
输出:
60 70 80 90 100
我有点忘记了c ++,但这里是psudo代码(实际上是javascript(:
notPresent=[];
for(i=0;i<array1.length;i++) {
found=false;
for(j=0;j<array2.length && !found;j++) {
if(array1[i]==array2[j]) found=true;
};
if(!found) {
notPresent.push(array1[i]);
}
}
这个想法是:循环访问第一个数组的元素并与第二个数组的所有元素进行比较(一旦找到就停止(,如果没有找到:缺少值,根据要求,推送到 notPresent 数组。
我提供以下解决方案:
- 运行时间:O(n * m(,n - arr1 的大小,m - arr2 的大小。内存: O(1(想法:第一个循环用于 arr1 中的元素,第二个循环用于 arr2 中的元素。如果元素匹配,那么您可以使用值(可能是 -1 000 000 或其他值(更改 arr1 中的元素,这将告诉您该元素显示在 arr2 中。例: 到达1 : [1, 2, 3], 到达2 : [10, 12 ,1]。 那么在解决方案之后,arr1 将是:[-1000000, 2, 3]。 它不需要额外的内存,因为您可以就地执行所有操作。
代码:
int main()
{
const int n = 10;
int arr1[n], arr2[n];
for(int i = 0; i < n; ++i)
{
std::cin >> arr1[i];
}
for(int j = 0; j < n; ++j)
{
std::cin >> arr2[j];
}
const int value = -1000000; // Better to use INT_MAX or INT_MIN
for(int i = 0; i < n; ++i)
{
for(int j = 0; j < m; ++j) // You should iterate through all elements
//in arr2 which size is m.
if(arr1[i] == arr2[j])
arr1[i] = value;
}
// Print elements which is in arr1 and not in arr2
for (int i = 0; i < n; i++){
std::cout << arr1[i] << ' ';
}
}
- 运行时:O(n*m(。内存: O(元素的大小在 arr1 中而不是在 arr2 中(想法:同样在这里,创建一个数组,显示哪些元素在 arr1 中,而不是在 arr2 中。然后遍历 arr1 的元素并与 arr2 的元素进行比较。如果元素与 arr2 的每个元素都不匹配,您可以保存地将其推送到 arr1。
法典:
#include <iostream>
int main()
{
const int n = 10;
int arr1[n], arr2[n];
for(int i = 0; i < n; ++i)
{
std::cin >> arr1[i];
}
for(int j = 0; j < n; ++j)
{
std::cin >> arr2[j];
}
vector<int> Array;
for(int i = 0; i < n; ++i){
int flag = 0; // flag initially 0 for every element in arr1
for(int j = 0; j < m; ++j){
if(arr1[i] == arr2[j]) // If it's match you make flag = 1
flag = 1;
}
if (flag == 0){ // It shows you that where wasn't match
Array.push_back(arr1[i]);
}
}
for (int i = 0; i < Array.size(); i++){
std::cout << Array[i] << ' ';
}
}
- 运行时:O(n( + O(m(。内存:O(n( **想法: **使用数据结构图。(为了理解它,我建议您在YouTube中搜索。有很多好视频(。尝试思考和实施自己(
如果您正在寻找内存效率的解决方案,即内存的 O(1(,那么您可以对第二个数组进行排序,并为第一个数组中的每个元素查找它是否存在于第二个数组中通过二进制搜索。时间复杂度:O(mlogm + nlogm)
其中n是第一个数组的大小,m是第二个数组的大小。
#include <iostream>
int main()
{
const int n = 10;
int arr1[n], arr2[n];
for(int i = 0; i < n; ++i)
{
std::cin >> arr1[i];
}
for(int j = 0; j < n; ++j)
{
std::cin >> arr2[j];
}
for(int i = 0; i < n; ++i)
{
int j;
for(j = 0; j < n; ++j)
if(arr1[i] == arr2[j])
break;
if(j == n)
std::cout << arr1[i] << " ";
}
}
相关文章:
- 我们可以访问一个不存在的联盟的成员吗
- C++:对不存在的命名空间使用命名空间指令
- g++ 说函数不存在,即使包含正确的标头
- 更新的矢量元素不打印
- 显式 std::exception_ptr 转换为 bool 不存在.VS2010 错误?
- C++ 尝试在不存在的构造函数中引用已删除的函数(使用 rapidJson)
- 查找第一个数组中不存在的元素
- 查找不存在的键时,unordered_map返回什么
- 转置矩阵:交换元素不会更改值
- 如何优化代码以返回最接近给定整数的数字,但给定列表中不存在?
- set::find 查找不存在的元素
- 如果键不存在,使用 [] 运算符访问 STL Map 元素会添加新元素
- lower_bound 在 C++ STL 中返回迭代器,即使元素不存在也是如此.如何避免这种情况?
- 如果路径的每个元素不存在,请为其创建一个目录
- 在C++中传递结构数组 - 被调用函数中不存在结构元素,如何正确传递向量?
- clang以 - 所有的标志在矢量中没有不存在的元素
- 有效地测试 QSet 是否包含元素,如果不存在,则插入
- 为什么 STL 向量的迭代器可以指向不存在的元素?
- 使用0来显示不存在元素的集合的子集
- 获取受不存在元素限制的QSet子集