LeetCode contest 183 5377. 将二进制表示减到 1 的步骤数

157 阅读2分钟

Table of Contents

一、中文版

二、英文版

三、My answer

四、解题报告


一、中文版

给你一个以二进制形式表示的数字 s 。请你返回按下述规则将其减少到 1 所需要的步骤数:

  • 如果当前数字为偶数,则将其除以 2 。
  • 如果当前数字为奇数,则将其加上 1 。

题目保证你总是可以按上述规则将测试用例变为 1 。

 

示例 1:

输入: s = "1101"
输出: 6
解释: "1101" 表示十进制数 13Step 1) 13 是奇数,加 1 得到 14 
Step 2) 14 是偶数,除 2 得到 7
Step 3) 7  是奇数,加 1 得到 8
Step 4) 8  是偶数,除 2 得到 4  
Step 5) 4  是偶数,除 2 得到 2 
Step 6) 2  是偶数,除 2 得到 1  

示例 2:

输入: s = "10"
输出: 1
解释: "10" 表示十进制数 2 。
Step 1) 2 是偶数,除 2 得到 1 

示例 3:

输入: s = "1"
输出: 0

 

提示:

  • 1 <= s.length <= 500
  • s 由字符 '0' 或 '1' 组成。
  • s[0] == '1'

二、英文版

Given a number s in their binary representation. Return the number of steps to reduce it to 1 under the following rules:

  • If the current number is even, you have to divide it by 2.
  • If the current number is odd, you have to add 1 to it.

It's guaranteed that you can always reach to one for all testcases.

 

Example 1:

Input: s = "1101"
Output: 6
Explanation: "1101" corressponds to number 13 in their decimal representation.
Step 1) 13 is odd, add 1 and obtain 14Step 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 4Step 5) 4 is even, divide by 2 and obtain 2Step 6) 2 is even, divide by 2 and obtain 1

Example 2:

Input: s = "10"
Output: 1
Explanation: "10" corressponds to number 2 in their decimal representation.
Step 1) 2 is even, divide by 2 and obtain 1

Example 3:

Input: s = "1"
Output: 0

 

Constraints:

  • 1 <= s.length <= 500
  • s consists of characters '0' or '1'
  • s[0] == '1'

三、My answer

class Solution:
    def numSteps(self, s: str) -> int:
        s_int = int(s,2)
        ans = 0
        while s_int != 1:
            if s_int % 2 == 1:
                s_int += 1
            else:
                s_int //= 2
            ans += 1
        return ans

四、解题报告

按照题意模拟即可。

注意:

s_int //= 2,不要写成 s_int /= 2,在例子 "1111011110000011100000110001011011110010111001010111110001" 报错。

可见虽然偶数时除以2,也明知道会整除,还是需要用 // 整除符号。