{"id":180,"date":"2017-09-09T13:11:14","date_gmt":"2017-09-09T20:11:14","guid":{"rendered":"http:\/\/zxi.mytechroad.com\/blog\/?p=180"},"modified":"2018-07-10T18:32:47","modified_gmt":"2018-07-11T01:32:47","slug":"leetcode-268-missing-number","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/math\/leetcode-268-missing-number\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 268. Missing Number"},"content":{"rendered":"<p><iframe loading=\"lazy\" title=\"LeetCode 268. Missing Number - \u82b1\u82b1\u9171 \u5237\u9898\u627e\u5de5\u4f5c EP44\" width=\"500\" height=\"375\" src=\"https:\/\/www.youtube.com\/embed\/z0p_FBatGWM?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe><\/p>\n<p><strong>Problem:<\/strong><\/p>\n<p>Given an array containing n distinct numbers taken from 0, 1, 2, &#8230;, n, find the one that is missing from the array.<\/p>\n<p>For example,<br \/>\nGiven nums = [0, 1, 3] return 2.<\/p>\n<p>Note:<br \/>\nYour algorithm should run in linear runtime complexity. Could you implement it using only constant extra space complexity?<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Idea:<\/strong><\/p>\n<p>sum \/ xor<\/p>\n<p><a href=\"http:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/09\/268-ep44.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-183\" src=\"http:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/09\/268-ep44.png\" alt=\"\" width=\"960\" height=\"540\" srcset=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/09\/268-ep44.png 960w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/09\/268-ep44-300x169.png 300w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/09\/268-ep44-768x432.png 768w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/09\/268-ep44-624x351.png 624w\" sizes=\"auto, (max-width: 960px) 100vw, 960px\" \/><\/a><\/p>\n<p><strong>Solution:<\/strong><\/p>\n<pre class=\"lang:c++ decode:true  \">\/\/ Author: Huahua\r\nclass Solution {\r\npublic:\r\n    \/\/ Solution 1: Sum\r\n    int missingNumber(vector&lt;int&gt;&amp; nums) {\r\n        int n = nums.size();\r\n        int x = (0+n)*(n+1)\/2 - accumulate(nums.begin(), nums.end(), 0);\r\n        return x;\r\n    }\r\n    \r\n    \/\/ Solution 2: XOR\r\n    int missingNumber(vector&lt;int&gt;&amp; nums) {\r\n        int n = nums.size();\r\n        int x = 0;\r\n        for(int i=1;i&lt;=n;++i)\r\n            x = x ^ i ^ nums[i-1];\r\n        return x;\r\n    }\r\n};<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Problem: Given an array containing n distinct numbers taken from 0, 1, 2, &#8230;, n, find the one that is missing from the array. For&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[49],"tags":[20,64,65,62,63],"class_list":["post-180","post","type-post","status-publish","format-standard","hentry","category-math","tag-array","tag-missing","tag-number","tag-sum","tag-xor","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/180","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=180"}],"version-history":[{"count":6,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/180\/revisions"}],"predecessor-version":[{"id":3062,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/180\/revisions\/3062"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=180"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=180"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=180"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}