<?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>graycode Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/graycode/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/graycode/</link>
	<description></description>
	<lastBuildDate>Sun, 04 Oct 2020 20:26:39 +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>graycode Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/graycode/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 1611. Minimum One Bit Operations to Make Integers Zero</title>
		<link>https://zxi.mytechroad.com/blog/math/leetcode-1611-minimum-one-bit-operations-to-make-integers-zero/</link>
					<comments>https://zxi.mytechroad.com/blog/math/leetcode-1611-minimum-one-bit-operations-to-make-integers-zero/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sun, 04 Oct 2020 20:26:05 +0000</pubDate>
				<category><![CDATA[Bit]]></category>
		<category><![CDATA[Math]]></category>
		<category><![CDATA[bit]]></category>
		<category><![CDATA[graycode]]></category>
		<category><![CDATA[hard]]></category>
		<category><![CDATA[math]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=7467</guid>

					<description><![CDATA[<p>Given an integer&#160;n, you must transform it into&#160;0&#160;using the following operations any number of times: Change the rightmost (0th) bit in the binary representation of&#160;n.&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/math/leetcode-1611-minimum-one-bit-operations-to-make-integers-zero/">花花酱 LeetCode 1611. Minimum One Bit Operations to Make Integers Zero</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 an integer&nbsp;<code>n</code>, you must transform it into&nbsp;<code>0</code>&nbsp;using the following operations any number of times:</p>



<ul><li>Change the rightmost (<code>0<sup>th</sup></code>) bit in the binary representation of&nbsp;<code>n</code>.</li><li>Change the&nbsp;<code>i<sup>th</sup></code>&nbsp;bit in the binary representation of&nbsp;<code>n</code>&nbsp;if the&nbsp;<code>(i-1)<sup>th</sup></code>&nbsp;bit is set to&nbsp;<code>1</code>&nbsp;and the&nbsp;<code>(i-2)<sup>th</sup></code>&nbsp;through&nbsp;<code>0<sup>th</sup></code>&nbsp;bits are set to&nbsp;<code>0</code>.</li></ul>



<p>Return&nbsp;<em>the minimum number of operations to transform&nbsp;</em><code>n</code><em>&nbsp;into&nbsp;</em><code>0</code><em>.</em></p>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> n = 0
<strong>Output:</strong> 0
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> n = 3
<strong>Output:</strong> 2
<strong>Explanation:</strong> The binary representation of 3 is "11".
"11" -&gt; "01" with the 2nd operation since the 0th bit is 1.
"01" -&gt; "00" with the 1st operation.
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> n = 6
<strong>Output:</strong> 4
<strong>Explanation:</strong> The binary representation of 6 is "110".
"110" -&gt; "010" with the 2nd operation since the 1st bit is 1 and 0th through 0th bits are 0.
"010" -&gt; "011" with the 1st operation.
"011" -&gt; "001" with the 2nd operation since the 0th bit is 1.
"001" -&gt; "000" with the 1st operation.
</pre>



<p><strong>Example 4:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> n = 9
<strong>Output:</strong> 14
</pre>



<p><strong>Example 5:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> n = 333
<strong>Output:</strong> 393
</pre>



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



<ul><li><code>0 &lt;= n &lt;= 10<sup>9</sup></code></li></ul>



<h2><strong>Solution 1: Graycode</strong></h2>



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



<p>Ans is the order of n in graycode.</p>



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

<pre class="crayon-plain-tag">// Author: Huahua
class Solution {
public:
  int minimumOneBitOperations(int n) {
    int ans = 0;
    while (n) {
      ans ^= n;
      n &gt;&gt;= 1;
    }
    return ans;
  }
};</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/math/leetcode-1611-minimum-one-bit-operations-to-make-integers-zero/">花花酱 LeetCode 1611. Minimum One Bit Operations to Make Integers Zero</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/math/leetcode-1611-minimum-one-bit-operations-to-make-integers-zero/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
