牛客题霸-数组中只出现一次的数字题解
程序员文章站
2022-03-08 15:51:53
...
排序+暴力枚举
class Solution {
public:
void FindNumsAppearOnce(vector<int> data,int* num1,int *num2) {
sort(data.begin(),data.end());
int top;
for(int i=0;i<data.size();i++){
int val=data[i];
int flag=0;
if(i-1>=0&&data[i-1]==val)flag=1;
if(i+1<data.size()&&data[i+1]==val)flag=1;
if(flag==0){
*num1=val;
top=val;
break;
}
}
for(int i=0;i<data.size();i++){
if(data[i]==top)continue;
int val=data[i];
int flag=0;
if(i-1>=0&&data[i-1]==val)flag=1;
if(i+1<data.size()&&data[i+1]==val)flag=1;
if(flag==0){
*num2=val;
break;
}
}
}
};