<?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>medieum Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/medieum/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/medieum/</link>
	<description></description>
	<lastBuildDate>Sun, 05 Apr 2020 07:25:16 +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>medieum Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/medieum/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 1404. Number of Steps to Reduce a Number in Binary Representation to One</title>
		<link>https://zxi.mytechroad.com/blog/bit/leetcode-1404-number-of-steps-to-reduce-a-number-in-binary-representation-to-one/</link>
					<comments>https://zxi.mytechroad.com/blog/bit/leetcode-1404-number-of-steps-to-reduce-a-number-in-binary-representation-to-one/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sun, 05 Apr 2020 07:22:53 +0000</pubDate>
				<category><![CDATA[Bit]]></category>
		<category><![CDATA[big integer]]></category>
		<category><![CDATA[binary string]]></category>
		<category><![CDATA[bit]]></category>
		<category><![CDATA[medieum]]></category>
		<category><![CDATA[O(n)]]></category>
		<category><![CDATA[string]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=6578</guid>

					<description><![CDATA[<p>Given a number&#160;s&#160;in their binary representation. Return the number of steps to reduce it to 1 under the following rules: If the current number is&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/bit/leetcode-1404-number-of-steps-to-reduce-a-number-in-binary-representation-to-one/">花花酱 LeetCode 1404. Number of Steps to Reduce a Number in Binary Representation to One</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 number&nbsp;<code>s</code>&nbsp;in their binary representation. Return the number of steps to reduce it to 1 under the following rules:</p>



<ul><li>If the current number is even, you have to divide it by 2.</li><li>If the current number is odd, you have to add 1 to it.</li></ul>



<p>It&#8217;s guaranteed that you can always reach to one for all testcases.</p>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> s = "1101"
<strong>Output:</strong> 6
<strong>Explanation:</strong> "1101" corressponds to number 13 in their decimal representation.
Step 1) 13 is odd, add 1 and obtain 14.&nbsp;
Step 2) 14 is even, divide by 2 and obtain 7.
Step 3) 7 is odd, add 1 and obtain 8.
Step 4) 8 is even, divide by 2 and obtain 4.&nbsp; 
Step 5) 4 is even, divide by 2 and obtain 2.&nbsp;
Step 6) 2 is even, divide by 2 and obtain 1.&nbsp; 
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> s = "10"
<strong>Output:</strong> 1
<strong>Explanation:</strong> "10" corressponds to number 2 in their decimal representation.
Step 1) 2 is even, divide by 2 and obtain 1.&nbsp; 
</pre>



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



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



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



<ul><li><code>1 &lt;= s.length&nbsp;&lt;= 500</code></li><li><code>s</code>&nbsp;consists of characters &#8216;0&#8217; or &#8216;1&#8217;</li><li><code>s[0] == '1'</code></li></ul>



<h2><strong>Solution: Simulation</strong></h2>



<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">// Author: Huahua
class Solution {
public:
  int numSteps(string s) {
    int ans = 0;
    int carry = 0;
    // The highest bit must be 1, 
    // process to the 2nd highest bit
    for (int i = s.length() - 1; i &gt; 0; --i) {
      if (s[i] - '0' + carry == 1) {
        ans += 2; // odd: +1, even: / 2
        carry = 1; // always has a carry
      } else {
        ans += 1; // even: / 2
        // carray remains e.g. 1 + 1 = 10, or 0 + 0 = 00
      }
    }
    // If there is a carry, then it's even, one more step.
    return ans + carry;
  }
};</pre>
</div></div>



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

<pre class="crayon-plain-tag"># Author: Huahua
class Solution:
  def numSteps(self, s: str) -&gt; int:
    ans, carry = 0, 0
    for i in range(1, len(s)):
      if ord(s[-i]) - ord('0') + carry == 1:
        ans += 2
        carry = 1
      else:
        ans += 1
    return ans + carry</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/bit/leetcode-1404-number-of-steps-to-reduce-a-number-in-binary-representation-to-one/">花花酱 LeetCode 1404. Number of Steps to Reduce a Number in Binary Representation to One</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-1404-number-of-steps-to-reduce-a-number-in-binary-representation-to-one/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
