LeetCode 题解工作台

将二进制表示减到 1 的步骤数

给你一个以二进制形式表示的数字 s 。请你返回按下述规则将其减少到 1 所需要的步骤数: 如果当前数字为偶数,则将其除以 2 。 如果当前数字为奇数,则将其加上 1 。 题目保证你总是可以按上述规则将测试用例变为 1 。 示例 1: 输入: s = "1101" 输出: 6 解释: "1101" 表…

category

3

题型

code_blocks

6

代码语言

hub

3

相关题

当前训练重点

中等 · string·结合·位运算·操作

bolt

答案摘要

我们模拟操作 和 ,同时维护一个进位 来表示当前是否有进位,初始时 $\textit{carry} = \text{false}$。 我们从字符串 的末尾开始向前遍历:

Interview AiBox logo

Interview AiBox 实时 AI 助手,陪你讲清 string·结合·位运算·操作 题型思路

试试 AiBox 面试助手arrow_forward
description

题目描述

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

  • 如果当前数字为偶数,则将其除以 2

  • 如果当前数字为奇数,则将其加上 1

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

 

示例 1:

输入:s = "1101"
输出:6
解释:"1101" 表示十进制数 13 。
Step 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'
lightbulb

解题思路

方法一:模拟

我们模拟操作 1122,同时维护一个进位 carry\textit{carry} 来表示当前是否有进位,初始时 carry=false\textit{carry} = \text{false}

我们从字符串 ss 的末尾开始向前遍历:

  • 如果 carry\textit{carry}true\text{true},则当前位 cc 需要加 11,如果 cc00,则加 11 后变为 11,同时 carry\textit{carry} 变为 false\text{false};如果 cc11,则加 11 后变为 00,同时 carry\textit{carry} 保持为 true\text{true}
  • 如果 cc11,则需要执行操作 11,即加 11,同时 carry\textit{carry} 变为 true\text{true}
  • 此时 cc00,则需要执行操作 22,即除以 22

当遍历结束后,如果 carry\textit{carry} 仍然为 true\text{true},则需要再执行一次操作 11

时间复杂度 O(n)O(n),其中 nn 是字符串 ss 的长度。空间复杂度 O(1)O(1)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class Solution:
    def numSteps(self, s: str) -> int:
        carry = False
        ans = 0
        for c in s[:0:-1]:
            if carry:
                if c == '0':
                    c = '1'
                    carry = False
                else:
                    c = '0'
            if c == '1':
                ans += 1
                carry = True
            ans += 1
        if carry:
            ans += 1
        return ans
speed

复杂度分析

指标
时间O(N)
空间O(1)
psychology

面试官常问的追问

外企场景
  • question_mark

    The candidate should demonstrate familiarity with string manipulation and bitwise operations.

  • question_mark

    Look for clarity in how they explain the iterative process and how they handle even/odd cases.

  • question_mark

    Check if they optimize the solution with minimal extra space usage.

warning

常见陷阱

外企场景
  • error

    Misunderstanding the even/odd rule and applying the wrong operation (e.g., dividing by 2 when the number is odd).

  • error

    Overcomplicating the solution by introducing unnecessary data structures or operations.

  • error

    Not efficiently handling large binary strings, leading to incorrect time complexity analysis.

swap_horiz

进阶变体

外企场景
  • arrow_right_alt

    Modify the problem to handle decimal numbers directly instead of binary strings.

  • arrow_right_alt

    Allow multiple ways of representing the number (e.g., hexadecimal or octal).

  • arrow_right_alt

    Introduce variations like counting steps for different bases, or changing the reduction rules.

help

常见问题

外企场景

将二进制表示减到 1 的步骤数题解:string·结合·位运算·操作 | LeetCode #1404 中等