{"id":1256,"date":"2017-12-15T21:32:50","date_gmt":"2017-12-16T05:32:50","guid":{"rendered":"http:\/\/zxi.mytechroad.com\/blog\/?p=1256"},"modified":"2017-12-17T09:43:34","modified_gmt":"2017-12-17T17:43:34","slug":"leetcode-540-single-element-in-a-sorted-array","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/algorithms\/binary-search\/leetcode-540-single-element-in-a-sorted-array\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 540. Single Element in a Sorted Array"},"content":{"rendered":"<p><iframe loading=\"lazy\" title=\"\u82b1\u82b1\u9171 LeetCode 540. Single Element in a Sorted Array - \u5237\u9898\u627e\u5de5\u4f5c EP134\" width=\"500\" height=\"375\" src=\"https:\/\/www.youtube.com\/embed\/uJa9Q-05JxY?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 a sorted array consisting of only integers where every element appears twice except for one element which appears once. Find this single element that appears only once.<\/p>\n<p><b>Example 1:<\/b><\/p>\n<pre class=\"\">Input: [1,1,2,3,3,4,4,8,8]\r\nOutput: 2\r\n<\/pre>\n<p><b>Example 2:<\/b><\/p>\n<pre class=\"\">Input: [3,3,7,7,10,11,11]\r\nOutput: 10\r\n<\/pre>\n<p><b>Note:<\/b>\u00a0Your solution should run in O(log n) time and O(1) space.<\/p>\n<p><strong>Idea:<\/strong><\/p>\n<p>Binary search.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1266\" src=\"http:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/12\/540-ep134.png\" alt=\"\" width=\"960\" height=\"540\" srcset=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/12\/540-ep134.png 960w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/12\/540-ep134-300x169.png 300w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/12\/540-ep134-768x432.png 768w\" sizes=\"auto, (max-width: 960px) 100vw, 960px\" \/><\/p>\n<p>Time complexity: O(logn)<\/p>\n<p>Space complexity: O(1)<\/p>\n<p><strong>Solution:\u00a0<\/strong><\/p>\n<p>C++<\/p>\n<pre class=\"lang:c++ decode:true \">\/\/ Author: Huahua\r\n\/\/ Runtime: 6 ms\r\nclass Solution {\r\npublic:\r\n    int singleNonDuplicate(vector&lt;int&gt;&amp; nums) {\r\n        int l = 0;\r\n        int r = nums.size();\r\n        while (l &lt; r) {\r\n            const int m = l + (r - l) \/ 2;\r\n            \/\/ int n = m % 2 == 0 ? m + 1 : m - 1;\r\n            const int n = m ^ 1;\r\n            if (nums[m] == nums[n])\r\n                l = m + 1;\r\n            else\r\n                r = m;\r\n        }\r\n        return nums[l];\r\n    }\r\n};<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Problem: Given a sorted array consisting of only integers where every element appears twice except for one element which appears once. Find this single element&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[149],"tags":[52,190],"class_list":["post-1256","post","type-post","status-publish","format-standard","hentry","category-binary-search","tag-binary-search","tag-single-number","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/1256","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=1256"}],"version-history":[{"count":4,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/1256\/revisions"}],"predecessor-version":[{"id":1267,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/1256\/revisions\/1267"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=1256"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=1256"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=1256"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}