AcWing 817. 数组去重
原题链接
中等
作者:
HelloZz
,
2021-01-09 21:22:25
,
所有人可见
,
阅读 226
算法1
(暴力枚举) $O(n^2)$
时间复杂度
参考文献
C++ 代码
#include<iostream>
using namespace std;
int unique(int a[] , int size){
int cnt = 0 ;
for(int i = 0 ; i < size ; i++){
bool is_exist = false;
for(int j = 0 ; j < i ; j++){
if(a[i] == a[j]){
is_exist = true;
break;
}
}
if(!is_exist) cnt++;
}
return cnt;
}
int main(){
int a[1000];
int n , size;
cin >> n >> size;
for(int i = 0 ; i < n ; i++) cin >> a[i];
cout << n -size + unique(a,size) << endl;
return 0;
}
算法2
(哈希表) $O(n^2)$
时间复杂度
参考文献
C++ 代码