{"id":9474,"date":"2022-02-04T17:13:01","date_gmt":"2022-02-05T01:13:01","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=9474"},"modified":"2022-02-04T17:13:19","modified_gmt":"2022-02-05T01:13:19","slug":"leetcode-2150-find-all-lonely-numbers-in-the-array","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/hashtable\/leetcode-2150-find-all-lonely-numbers-in-the-array\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 2150. Find All Lonely Numbers in the Array"},"content":{"rendered":"\n<p>You are given an integer array&nbsp;<code>nums<\/code>. A number&nbsp;<code>x<\/code>&nbsp;is&nbsp;<strong>lonely<\/strong>&nbsp;when it appears only&nbsp;<strong>once<\/strong>, and no&nbsp;<strong>adjacent<\/strong>&nbsp;numbers (i.e.&nbsp;<code>x + 1<\/code>&nbsp;and&nbsp;<code>x - 1)<\/code>&nbsp;appear in the array.<\/p>\n\n\n\n<p>Return&nbsp;<em><strong>all<\/strong>&nbsp;lonely numbers in&nbsp;<\/em><code>nums<\/code>. You may return the answer in&nbsp;<strong>any order<\/strong>.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> nums = [10,6,5,8]\n<strong>Output:<\/strong> [10,8]\n<strong>Explanation:<\/strong> \n- 10 is a lonely number since it appears exactly once and 9 and 11 does not appear in nums.\n- 8 is a lonely number since it appears exactly once and 7 and 9 does not appear in nums.\n- 5 is not a lonely number since 6 appears in nums and vice versa.\nHence, the lonely numbers in nums are [10, 8].\nNote that [8, 10] may also be returned.\n<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> nums = [1,3,5,3]\n<strong>Output:<\/strong> [1,5]\n<strong>Explanation:<\/strong> \n- 1 is a lonely number since it appears exactly once and 0 and 2 does not appear in nums.\n- 5 is a lonely number since it appears exactly once and 4 and 6 does not appear in nums.\n- 3 is not a lonely number since it appears twice.\nHence, the lonely numbers in nums are [1, 5].\nNote that [5, 1] may also be returned.\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>1 &lt;= nums.length &lt;= 10<sup>5<\/sup><\/code><\/li><li><code>0 &lt;= nums[i] &lt;= 10<sup>6<\/sup><\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Counter<\/strong><\/h2>\n\n\n\n<p>Computer the frequency of each number in the array, for a given number x with freq = 1, check freq of (x &#8211; 1) and (x + 1), if both of them are zero then x is lonely.<\/p>\n\n\n\n<p>Time complexity: O(n)<br>Space complexity: O(n)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\n\/\/ Author: Huahua\nclass Solution {\npublic:\n  vector<int> findLonely(vector<int>& nums) {\n    unordered_map<int, int> m;\n    for (int x : nums)\n      ++m[x];\n    vector<int> ans;\n    for (const auto [x, c] : m)\n      if (c == 1 && !m.count(x + 1) && !m.count(x - 1))\n        ans.push_back(x);\n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>You are given an integer array&nbsp;nums. A number&nbsp;x&nbsp;is&nbsp;lonely&nbsp;when it appears only&nbsp;once, and no&nbsp;adjacent&nbsp;numbers (i.e.&nbsp;x + 1&nbsp;and&nbsp;x &#8211; 1)&nbsp;appear in the array. Return&nbsp;all&nbsp;lonely numbers in&nbsp;nums. You&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[70],"tags":[254,82,177],"class_list":["post-9474","post","type-post","status-publish","format-standard","hentry","category-hashtable","tag-counter","tag-hashtable","tag-medium","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9474","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=9474"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9474\/revisions"}],"predecessor-version":[{"id":9476,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9474\/revisions\/9476"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=9474"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=9474"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=9474"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}