{"id":2315,"date":"2018-03-23T00:33:44","date_gmt":"2018-03-23T07:33:44","guid":{"rendered":"http:\/\/zxi.mytechroad.com\/blog\/?p=2315"},"modified":"2018-03-23T00:33:52","modified_gmt":"2018-03-23T07:33:52","slug":"leetcode-448-find-all-numbers-disappeared-in-an-array","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/algorithms\/array\/leetcode-448-find-all-numbers-disappeared-in-an-array\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 448. Find All Numbers Disappeared in an Array"},"content":{"rendered":"<h1>Problem<\/h1>\n<div class=\"question-description\">\n<div>\n<p>Given an array of integers where 1 \u2264 a[i] \u2264\u00a0<i>n<\/i>\u00a0(<i>n<\/i>\u00a0= size of array), some elements appear twice and others appear once.<\/p>\n<p>Find all the elements of [1,\u00a0<i>n<\/i>] inclusive that do not appear in this array.<\/p>\n<p>Could you do it without extra space and in O(<i>n<\/i>) runtime? You may assume the returned list does not count as extra space.<\/p>\n<p><b>Example:<\/b><\/p>\n<pre class=\"crayon:false \"><b>Input:<\/b>\r\n[4,3,2,7,8,2,3,1]\r\n\r\n<b>Output:<\/b>\r\n[5,6]\r\n<\/pre>\n<\/div>\n<\/div>\n<h1><strong>Solution<\/strong><\/h1>\n<p>Time complexity: O(n)<\/p>\n<p>Space complexity: O(1)<\/p>\n<p>C++<\/p>\n<pre class=\"lang:c++ decode:true \">\/\/ Author: Huahua\r\n\/\/ Running time: 128 ms (beats 93.70%)\r\nclass Solution {\r\npublic:\r\n  vector&lt;int&gt; findDisappearedNumbers(vector&lt;int&gt;&amp; nums) {\r\n    for (size_t i = 0; i != nums.size(); ++i) {\r\n      int index = abs(nums[i]) - 1;\r\n      if (nums[index] &gt; 0) nums[index] *= -1;\r\n    }\r\n    vector&lt;int&gt; ans;\r\n    for (size_t i = 0; i != nums.size(); ++i)\r\n      if (nums[i] &gt; 0) ans.push_back(i + 1);\r\n    return ans;\r\n  }\r\n};<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Problem Given an array of integers where 1 \u2264 a[i] \u2264\u00a0n\u00a0(n\u00a0= size of array), some elements appear twice and others appear once. Find all the&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[184],"tags":[20,222,64],"class_list":["post-2315","post","type-post","status-publish","format-standard","hentry","category-array","tag-array","tag-easy","tag-missing","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/2315","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=2315"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/2315\/revisions"}],"predecessor-version":[{"id":2317,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/2315\/revisions\/2317"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=2315"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=2315"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=2315"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}