<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>single number Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/single-number/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/single-number/</link>
	<description></description>
	<lastBuildDate>Sun, 17 Dec 2017 17:43:34 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.0.8</generator>

<image>
	<url>https://zxi.mytechroad.com/blog/wp-content/uploads/2017/09/cropped-photo-32x32.jpg</url>
	<title>single number Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/single-number/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 540. Single Element in a Sorted Array</title>
		<link>https://zxi.mytechroad.com/blog/algorithms/binary-search/leetcode-540-single-element-in-a-sorted-array/</link>
					<comments>https://zxi.mytechroad.com/blog/algorithms/binary-search/leetcode-540-single-element-in-a-sorted-array/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sat, 16 Dec 2017 05:32:50 +0000</pubDate>
				<category><![CDATA[Binary Search]]></category>
		<category><![CDATA[binary search]]></category>
		<category><![CDATA[single number]]></category>
		<guid isPermaLink="false">http://zxi.mytechroad.com/blog/?p=1256</guid>

					<description><![CDATA[<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>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/algorithms/binary-search/leetcode-540-single-element-in-a-sorted-array/">花花酱 LeetCode 540. Single Element in a Sorted Array</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p><iframe width="500" height="375" src="https://www.youtube.com/embed/uJa9Q-05JxY?feature=oembed" frameborder="0" gesture="media" allow="encrypted-media" allowfullscreen></iframe></p>
<p><strong>Problem:</strong></p>
<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>
<p><b>Example 1:</b></p><pre class="crayon-plain-tag">Input: [1,1,2,3,3,4,4,8,8]
Output: 2</pre><p><b>Example 2:</b></p><pre class="crayon-plain-tag">Input: [3,3,7,7,10,11,11]
Output: 10</pre><p><b>Note:</b> Your solution should run in O(log n) time and O(1) space.</p>
<p><strong>Idea:</strong></p>
<p>Binary search.</p>
<p><img 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="(max-width: 960px) 100vw, 960px" /></p>
<p>Time complexity: O(logn)</p>
<p>Space complexity: O(1)</p>
<p><strong>Solution: </strong></p>
<p>C++</p><pre class="crayon-plain-tag">// Author: Huahua
// Runtime: 6 ms
class Solution {
public:
    int singleNonDuplicate(vector&lt;int&gt;&amp; nums) {
        int l = 0;
        int r = nums.size();
        while (l &lt; r) {
            const int m = l + (r - l) / 2;
            // int n = m % 2 == 0 ? m + 1 : m - 1;
            const int n = m ^ 1;
            if (nums[m] == nums[n])
                l = m + 1;
            else
                r = m;
        }
        return nums[l];
    }
};</pre><p>&nbsp;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/algorithms/binary-search/leetcode-540-single-element-in-a-sorted-array/">花花酱 LeetCode 540. Single Element in a Sorted Array</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://zxi.mytechroad.com/blog/algorithms/binary-search/leetcode-540-single-element-in-a-sorted-array/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
