class Solution {
public:
vector<int> singleNumber(vector<int>& nums) {
vector<int> ans;
sort(nums.begin(), nums.end());
if (nums[0] != nums[1]){
ans.push_back(nums[0]);
}
if (nums[nums.size() - 1] != nums[nums.size() - 2]){
ans.push_back(nums[nums.size() - 1]);
}
for(int i = 1; i < nums.size()-1; i++){
if ((nums[i] != nums[i - 1]) && (nums[i] != nums[i + 1])){
ans.push_back(nums[i]);
}
}
return ans;
}
};
只出现一次的数字,任意顺序返回
最新推荐文章于 2023-01-30 11:13:41 发布