2150.cpp (536B)
1 class Solution { 2 public: 3 vector<int> findLonely(vector<int> &nums) { 4 sort(begin(nums), end(nums)); 5 6 vector<int> res; 7 const int n = nums.size() - 1; 8 for (int i = 0; i <= n; i++) { 9 if (i < n && nums[i] == nums[i + 1]) continue; 10 if (i > 0 && nums[i] == nums[i - 1]) continue; 11 if (i > 0 && nums[i] - 1 == nums[i - 1]) continue; 12 if (i < n && nums[i] + 1 == nums[i + 1]) continue; 13 res.push_back(nums[i]); 14 } 15 return res; 16 } 17 };