返回介绍

solution / 2100-2199 / 2150.Find All Lonely Numbers in the Array / README_EN

发布于 2024-06-17 01:03:09 字数 4162 浏览 0 评论 0 收藏 0

2150. Find All Lonely Numbers in the Array

中文文档

Description

You are given an integer array nums. A number x is lonely when it appears only once, and no adjacent numbers (i.e. x + 1 and x - 1) appear in the array.

Return _all lonely numbers in _nums. You may return the answer in any order.

 

Example 1:

Input: nums = [10,6,5,8]
Output: [10,8]
Explanation: 
- 10 is a lonely number since it appears exactly once and 9 and 11 does not appear in nums.
- 8 is a lonely number since it appears exactly once and 7 and 9 does not appear in nums.
- 5 is not a lonely number since 6 appears in nums and vice versa.
Hence, the lonely numbers in nums are [10, 8].
Note that [8, 10] may also be returned.

Example 2:

Input: nums = [1,3,5,3]
Output: [1,5]
Explanation: 
- 1 is a lonely number since it appears exactly once and 0 and 2 does not appear in nums.
- 5 is a lonely number since it appears exactly once and 4 and 6 does not appear in nums.
- 3 is not a lonely number since it appears twice.
Hence, the lonely numbers in nums are [1, 5].
Note that [5, 1] may also be returned.

 

Constraints:

  • 1 <= nums.length <= 105
  • 0 <= nums[i] <= 106

Solutions

Solution 1

class Solution:
  def findLonely(self, nums: List[int]) -> List[int]:
    counter = Counter(nums)
    ans = []
    for num, cnt in counter.items():
      if cnt == 1 and counter[num - 1] == 0 and counter[num + 1] == 0:
        ans.append(num)
    return ans
class Solution {

  public List<Integer> findLonely(int[] nums) {
    Map<Integer, Integer> counter = new HashMap<>();
    for (int num : nums) {
      counter.put(num, counter.getOrDefault(num, 0) + 1);
    }
    List<Integer> ans = new ArrayList<>();
    counter.forEach((k, v) -> {
      if (v == 1 && !counter.containsKey(k - 1) && !counter.containsKey(k + 1)) {
        ans.add(k);
      }
    });
    return ans;
  }
}
class Solution {
public:
  vector<int> findLonely(vector<int>& nums) {
    unordered_map<int, int> counter;
    for (int num : nums) ++counter[num];
    vector<int> ans;
    for (auto& e : counter) {
      int k = e.first, v = e.second;
      if (v == 1 && !counter.count(k - 1) && !counter.count(k + 1)) ans.push_back(k);
    }
    return ans;
  }
};
func findLonely(nums []int) []int {
  counter := make(map[int]int)
  for _, num := range nums {
    counter[num]++
  }
  var ans []int
  for k, v := range counter {
    if v == 1 && counter[k-1] == 0 && counter[k+1] == 0 {
      ans = append(ans, k)
    }
  }
  return ans
}
function findLonely(nums: number[]): number[] {
  let hashMap: Map<number, number> = new Map();
  for (let num of nums) {
    hashMap.set(num, (hashMap.get(num) || 0) + 1);
  }
  let ans: Array<number> = [];
  for (let [num, count] of hashMap.entries()) {
    if (count == 1 && !hashMap.get(num - 1) && !hashMap.get(num + 1)) {
      ans.push(num);
    }
  }
  return ans;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文