<?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>O(1) space Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/o1-space/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/o1-space/</link>
	<description></description>
	<lastBuildDate>Sun, 28 Nov 2021 05:12:58 +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>O(1) space Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/o1-space/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 136. Single Number</title>
		<link>https://zxi.mytechroad.com/blog/bit/leetcode-136-single-number/</link>
					<comments>https://zxi.mytechroad.com/blog/bit/leetcode-136-single-number/#comments</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sun, 28 Nov 2021 05:04:42 +0000</pubDate>
				<category><![CDATA[Bit]]></category>
		<category><![CDATA[bit]]></category>
		<category><![CDATA[easy]]></category>
		<category><![CDATA[missing]]></category>
		<category><![CDATA[O(1) space]]></category>
		<category><![CDATA[xor]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=8843</guid>

					<description><![CDATA[<p>Given a&#160;non-empty&#160;array of integers&#160;nums, every element appears&#160;twice&#160;except for one. Find that single one. You must&#160;implement a solution with a linear runtime complexity and use&#160;only constant&#160;extra&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/bit/leetcode-136-single-number/">花花酱 LeetCode 136. Single Number</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>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>



<p>You must&nbsp;implement a solution with a linear runtime complexity and use&nbsp;only constant&nbsp;extra space.</p>



<p><strong>Example 1:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> nums = [2,2,1]
<strong>Output:</strong> 1
</pre>



<p><strong>Example 2:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> nums = [4,1,2,1,2]
<strong>Output:</strong> 4
</pre>



<p><strong>Example 3:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> nums = [1]
<strong>Output:</strong> 1
</pre>



<p><strong>Constraints:</strong></p>



<ul><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>



<h2><strong>Solution: XOR</strong></h2>



<p>single_number ^ a ^ b ^ c ^ &#8230; ^ a ^ b ^ c &#8230; = single_number</p>



<p>Time complexity: O(n)<br>Space complexity: O(1)</p>



<div class="responsive-tabs">
<h2 class="tabtitle">C++</h2>
<div class="tabcontent">

<pre class="crayon-plain-tag">class Solution {
public:
  int singleNumber(vector&lt;int&gt;&amp; nums) {
    int ans = 0;
    for (int x : nums)
      ans ^= x;
    return ans;
  }
};</pre>
</div></div>



<h2><strong>Related Problems</strong></h2>



<ul><li><a href="https://zxi.mytechroad.com/blog/bit/leetcode-137-single-number-ii/" data-type="post" data-id="8846">花花酱 LeetCode 137. Single Number II</a></li></ul>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/bit/leetcode-136-single-number/">花花酱 LeetCode 136. Single Number</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/bit/leetcode-136-single-number/feed/</wfw:commentRss>
			<slash:comments>1</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 287. Find the Duplicate Number</title>
		<link>https://zxi.mytechroad.com/blog/algorithms/binary-search/leetcode-287-find-the-duplicate-number/</link>
					<comments>https://zxi.mytechroad.com/blog/algorithms/binary-search/leetcode-287-find-the-duplicate-number/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sun, 15 Jul 2018 05:05:57 +0000</pubDate>
				<category><![CDATA[Binary Search]]></category>
		<category><![CDATA[binary search]]></category>
		<category><![CDATA[duplicate]]></category>
		<category><![CDATA[medium]]></category>
		<category><![CDATA[O(1) space]]></category>
		<guid isPermaLink="false">http://zxi.mytechroad.com/blog/?p=3159</guid>

					<description><![CDATA[<p>Problem Given an array nums containing n + 1 integers where each integer is between 1 and n (inclusive), prove that at least one duplicate number must exist. Assume that there&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/algorithms/binary-search/leetcode-287-find-the-duplicate-number/">花花酱 LeetCode 287. Find the Duplicate Number</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></description>
										<content:encoded><![CDATA[<h1><strong>Problem</strong></h1>
<p>Given an array <i>nums</i> containing <i>n</i> + 1 integers where each integer is between 1 and <i>n</i> (inclusive), prove that at least one duplicate number must exist. Assume that there is only one duplicate number, find the duplicate one.</p>
<p><b>Example 1:</b></p>
<pre class="crayon:false"><b>Input:</b> <code>[1,3,4,2,2]
</code><b>Output:</b> 2</pre>
<p><b>Example 2:</b></p>
<pre class="crayon:false "><b>Input:</b> [3,1,3,4,2]
<b>Output:</b> 3</pre>
<p><b>Note:</b></p>
<ol>
<li>You <b>must not</b> modify the array (assume the array is read only).</li>
<li>You must use only constant, <i>O</i>(1) extra space.</li>
<li>Your runtime complexity should be less than <em>O</em>(<em>n</em><sup>2</sup>).</li>
<li>There is only one duplicate number in the array, but it could be repeated more than once.</li>
</ol>
<h1><strong>Solution1: Binary Search</strong></h1>
<p>Time complexity: O(nlogn)</p>
<p>Space complexity: O(1)</p>
<p>Find the smallest m such that len(nums &lt;= m) &gt; m, which means m is the duplicate number.</p>
<p>In the sorted form [1, 2, &#8230;, m1, m2, m + 1, &#8230;, n]</p>
<p>There are m+1 numbers &lt;= m</p><pre class="crayon-plain-tag">// Author: Huahua
// Running time: 8 ms
class Solution {
public:
  int findDuplicate(vector&lt;int&gt;&amp; nums) {
    int l = 1;
    int r = nums.size();
    while (l &lt; r) {
      int m = (r - l) / 2 + l;
      int count = 0; // len(nums &lt;= m)
      for (int num : nums)
        if (num &lt;= m) ++count;
      if (count &lt;= m)
        l = m + 1;
      else
        r = m;
    }
    return l;
  }
};</pre><p></p>
<h1><strong>Solution: Linked list cycle</strong></h1>
<p>Convert the problem to find the entry point of the cycle in a linked list.</p>
<p>Take the number in the array as the index of next node.</p>
<p>[1,3,4,2,2]</p>
<p>0-&gt;1-&gt;3-&gt;2-&gt;4-&gt;2 cycle: 2-&gt;4-&gt;2</p>
<p>[3,1,3,4,2]</p>
<p>0-&gt;3-&gt;4-&gt;2-&gt;3-&gt;4-&gt;2 cycle 3-&gt;4-&gt;2-&gt;3</p>
<p>Time complexity: O(n)</p>
<p>Space complexity: O(1)</p><pre class="crayon-plain-tag">// Author: Huahua
// Running time: 4 ms
class Solution {
public:
  int findDuplicate(vector&lt;int&gt;&amp; nums) {
    int slow = 0;
    int fast = 0;
    while (true) {
      slow = nums[slow];
      fast = nums[nums[fast]];
      if (slow == fast) break;
    }
    fast = 0;
    while (fast != slow) {
      slow = nums[slow];
      fast = nums[fast];
    }
    return slow;
  }
};</pre><p>&nbsp;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/algorithms/binary-search/leetcode-287-find-the-duplicate-number/">花花酱 LeetCode 287. Find the Duplicate Number</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-287-find-the-duplicate-number/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
