class Solution {
public:
vector<int> findLonely(vector<int>& nums) {
}
};
2150. 找出数组中的所有孤独数字
给你一个整数数组 nums
。如果数字 x
在数组中仅出现 一次 ,且没有 相邻 数字(即,x + 1
和 x - 1
)出现在数组中,则认为数字 x
是 孤独数字 。
返回 nums
中的 所有 孤独数字。你可以按 任何顺序 返回答案。
示例 1:
输入:nums = [10,6,5,8] 输出:[10,8] 解释: - 10 是一个孤独数字,因为它只出现一次,并且 9 和 11 没有在 nums 中出现。 - 8 是一个孤独数字,因为它只出现一次,并且 7 和 9 没有在 nums 中出现。 - 5 不是一个孤独数字,因为 6 出现在 nums 中,反之亦然。 因此,nums 中的孤独数字是 [10, 8] 。 注意,也可以返回 [8, 10] 。
示例 2:
输入:nums = [1,3,5,3] 输出:[1,5] 解释: - 1 是一个孤独数字,因为它只出现一次,并且 0 和 2 没有在 nums 中出现。 - 5 是一个孤独数字,因为它只出现一次,并且 4 和 6 没有在 nums 中出现。 - 3 不是一个孤独数字,因为它出现两次。 因此,nums 中的孤独数字是 [1, 5] 。 注意,也可以返回 [5, 1] 。
提示:
1 <= nums.length <= 105
0 <= nums[i] <= 106
原站题解
golang 解法, 执行用时: 232 ms, 内存消耗: 11.9 MB, 提交时间: 2022-12-04 13:40:15
func findLonely(nums []int) []int { cnts := map[int]int{} for _, num := range nums { cnts[num]++ } ans := []int{} for k, v := range cnts { if v == 1 && cnts[k - 1] == 0 && cnts[k + 1] == 0{ ans = append(ans, k) } } return ans }
python3 解法, 执行用时: 264 ms, 内存消耗: 27.3 MB, 提交时间: 2022-12-04 13:39:34
class Solution: def findLonely(self, nums: List[int]) -> List[int]: nums += [-float("INF"), float("INF")] # 避免额外判断边界条件 nums.sort() n = len(nums) res = [] for i in range(1, n - 1): if nums[i] - nums[i-1] > 1 and nums[i+1] - nums[i] > 1: res.append(nums[i]) return res
python3 解法, 执行用时: 208 ms, 内存消耗: 34.9 MB, 提交时间: 2022-12-04 13:36:42
class Solution: def findLonely(self, nums: List[int]) -> List[int]: c = Counter(nums) ans = [] for k, v in c.items(): if v == 1 and k-1 not in c and k+1 not in c: ans.append(k) return ans