「这是我参与11月更文挑战的第25天,活动详情查看:2021最后一次更文挑战」
描述
Given an integer n, you must transform it into 0 using the following operations any number of times:
- Change the rightmost (0th) bit in the binary representation of n.
- Change the ith bit in the binary representation of n if the (i-1)th bit is set to 1 and the (i-2)th through 0th bits are set to 0.
Return the minimum number of operations to transform n into 0.
Example 1:
1 | ini复制代码Input: n = 0 |
Example 2:
1 | vbnet复制代码Input: n = 3 |
Example 3:
1 | vbnet复制代码Input: n = 6 |
Example 4:
1 | ini复制代码Input: n = 9 |
Example 5:
1 | ini复制代码Input: n = 333 |
Note:
1 | 复制代码0 <= n <= 109 |
解析
根据题意,给定一个整数 n,必须使用任意次数的以下操作将其转换为 0:
- 更改 n 的二进制表示最右边的位。可以 0 变为 1 ,也可以 1 变为 0 。
- 如果第 (i-1) 位设置为 1 并且第 (i-2) 到第 0 位设置为 0,则更改 n 的二进制表示中的第 i 位。可以 0 变为 1 ,也可以 1 变为 0 。
注意题目中的二进制位的索引都是从右向左的,返回将 n 转换为 0 的最小操作数。因为方法一只是将最右边的 0 和 1 互换,无法对前面的字符进行操作,所以关键就是巧用方法二进行变化,假如我们举例,将 101011 变为 000000 ,其最简单的思路就是递归:
- (1)101011 第一位为 1 ,想要将其变为 100000 ,就调用自定义的 convert 函数,该函数的功能就是找出将 01011 变为 10000 的最少次数
- (2)应用方法二将变化之后的 110000 变为 010000 进行了 1 次操作,然后计算将 10000 变为 00000 的次数,和上面同样的方法,将 0000 通过 convert 函数变为 1000 ,在进行相同的操作,直到最后变为 000000
- (3)所以定义递归函数 dfs ,表示对输入二进制的最少次数操作,将上面的过程表示出来就是 dfs(101011) = convert(01011) + 1 + dfs(10000)
但是 convert 有两种情况:
- 第一种情况是二进制的第一个数字是 1 ,如 1110 。那直接调用 dfs(110) 即可
- 第二种情况是二进制的第一个数字是 0 ,如 0111 ,又是需要递归 :convert(0111) = convert(111) + 1 + dfs(100)
解答
1 | python复制代码class Solution(object): |
运行结果
1 | erlang复制代码Runtime: 44 ms, faster than 5.17% of Python online submissions for Minimum One Bit Operations to Make Integers Zero. |
原题链接:leetcode.com/problems/mi…
您的支持是我最大的动力
本文转载自: 掘金