{"id":6578,"date":"2020-04-05T00:22:53","date_gmt":"2020-04-05T07:22:53","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=6578"},"modified":"2020-04-05T00:25:16","modified_gmt":"2020-04-05T07:25:16","slug":"leetcode-1404-number-of-steps-to-reduce-a-number-in-binary-representation-to-one","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/bit\/leetcode-1404-number-of-steps-to-reduce-a-number-in-binary-representation-to-one\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1404. Number of Steps to Reduce a Number in Binary Representation to One"},"content":{"rendered":"\n<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>\n\n\n\n<ul class=\"wp-block-list\"><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>\n\n\n\n<p>It&#8217;s guaranteed that you can always reach to one for all testcases.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> s = \"1101\"\n<strong>Output:<\/strong> 6\n<strong>Explanation:<\/strong> \"1101\" corressponds to number 13 in their decimal representation.\nStep 1) 13 is odd, add 1 and obtain 14.&nbsp;\nStep 2) 14 is even, divide by 2 and obtain 7.\nStep 3) 7 is odd, add 1 and obtain 8.\nStep 4) 8 is even, divide by 2 and obtain 4.&nbsp; \nStep 5) 4 is even, divide by 2 and obtain 2.&nbsp;\nStep 6) 2 is even, divide by 2 and obtain 1.&nbsp; \n<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> s = \"10\"\n<strong>Output:<\/strong> 1\n<strong>Explanation:<\/strong> \"10\" corressponds to number 2 in their decimal representation.\nStep 1) 2 is even, divide by 2 and obtain 1.&nbsp; \n<\/pre>\n\n\n\n<p><strong>Example 3:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> s = \"1\"\n<strong>Output:<\/strong> 0\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><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>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Simulation<\/strong><\/h2>\n\n\n\n<p>Time complexity: O(n)<br>Space complexity: O(1)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\/\/ Author: Huahua\nclass Solution {\npublic:\n  int numSteps(string s) {\n    int ans = 0;\n    int carry = 0;\n    \/\/ The highest bit must be 1, \n    \/\/ process to the 2nd highest bit\n    for (int i = s.length() - 1; i > 0; --i) {\n      if (s[i] - '0' + carry == 1) {\n        ans += 2; \/\/ odd: +1, even: \/ 2\n        carry = 1; \/\/ always has a carry\n      } else {\n        ans += 1; \/\/ even: \/ 2\n        \/\/ carray remains e.g. 1 + 1 = 10, or 0 + 0 = 00\n      }\n    }\n    \/\/ If there is a carry, then it's even, one more step.\n    return ans + carry;\n  }\n};\n<\/pre>\n<\/div><\/div>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">Python3<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"python\"># Author: Huahua\nclass Solution:\n  def numSteps(self, s: str) -> int:\n    ans, carry = 0, 0\n    for i in range(1, len(s)):\n      if ord(s[-i]) - ord('0') + carry == 1:\n        ans += 2\n        carry = 1\n      else:\n        ans += 1\n    return ans + carry\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Given a number&nbsp;s&nbsp;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>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[126],"tags":[347,235,16,577,376,4],"class_list":["post-6578","post","type-post","status-publish","format-standard","hentry","category-bit","tag-big-integer","tag-binary-string","tag-bit","tag-medieum","tag-on","tag-string","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6578","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=6578"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6578\/revisions"}],"predecessor-version":[{"id":6580,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6578\/revisions\/6580"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=6578"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=6578"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=6578"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}