{"id":8843,"date":"2021-11-27T21:04:42","date_gmt":"2021-11-28T05:04:42","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=8843"},"modified":"2021-11-27T21:12:58","modified_gmt":"2021-11-28T05:12:58","slug":"leetcode-136-single-number","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/bit\/leetcode-136-single-number\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 136. Single Number"},"content":{"rendered":"\n<p>Given a&nbsp;<strong>non-empty<\/strong>&nbsp;array of integers&nbsp;<code>nums<\/code>, every element appears&nbsp;<em>twice<\/em>&nbsp;except for one. Find that single one.<\/p>\n\n\n\n<p>You must&nbsp;implement a solution with a linear runtime complexity and use&nbsp;only constant&nbsp;extra space.<\/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 = [2,2,1]\n<strong>Output:<\/strong> 1\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 = [4,1,2,1,2]\n<strong>Output:<\/strong> 4\n<\/pre>\n\n\n\n<p><strong>Example 3:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> nums = [1]\n<strong>Output:<\/strong> 1\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;= 3 * 10<sup>4<\/sup><\/code><\/li><li><code>-3 * 10<sup>4<\/sup>&nbsp;&lt;= nums[i] &lt;= 3 * 10<sup>4<\/sup><\/code><\/li><li>Each element in the array appears twice except for one element which appears only once.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: XOR<\/strong><\/h2>\n\n\n\n<p>single_number ^ a ^ b ^ c ^ &#8230; ^ a ^ b ^ c &#8230; = single_number<\/p>\n\n\n\n<p>Time complexity: O(n)<br>Space complexity: O(1)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\nclass Solution {\npublic:\n  int singleNumber(vector<int>& nums) {\n    int ans = 0;\n    for (int x : nums)\n      ans ^= x;\n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Related Problems<\/strong><\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li><a href=\"https:\/\/zxi.mytechroad.com\/blog\/bit\/leetcode-137-single-number-ii\/\" data-type=\"post\" data-id=\"8846\">\u82b1\u82b1\u9171 LeetCode 137. Single Number II<\/a><\/li><\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Given a&nbsp;non-empty&nbsp;array of integers&nbsp;nums, every element appears&nbsp;twice&nbsp;except for one. Find that single one. You must&nbsp;implement a solution with a linear runtime complexity and use&nbsp;only constant&nbsp;extra&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[126],"tags":[16,222,64,334,63],"class_list":["post-8843","post","type-post","status-publish","format-standard","hentry","category-bit","tag-bit","tag-easy","tag-missing","tag-o1-space","tag-xor","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8843","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=8843"}],"version-history":[{"count":3,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8843\/revisions"}],"predecessor-version":[{"id":8849,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8843\/revisions\/8849"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=8843"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=8843"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=8843"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}