题目地址:https://leetcode-cn.com/problems/number-of-steps-to-reduce-a-number-in-binary-representation-to-one/ 题目描述 给你一个以二进制形式表示的数字s。请你返回按下述规则将其减少到 1 所需要的步骤数: 如果当前数字为偶数,则将其除以 2 。 如果当前数字为奇数,则将其加上 1 。 题目保证你总是...
classSolution{ public:intnumberOfSteps(intnum) {returnnum==0?0: log2(num) + bitset<32>(num).count(); } }; Github 同步地址: https://github.com/grandyang/leetcode/issues/1342 类似题目: Minimum Moves to Reach Target Score Count Operations to Obtain Zero 参考资料: https://leetcode.com/...
Given the binary representation of an integer as a string s, 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 is guaranteed that you can...
Given a non-negative integer num, return the number of steps to reduce it to zero. If the current number is even, you have to divide it by 2, otherwise, you have to subtract 1 from it.Example 1:Input: num = 14Output: 6Explanation:Step 1) 14 is even; divide by 2 and obtain 7....
链接:https://leetcode-cn.com/problems/number-of-steps-to-reduce-a-number-in-binary-representation-to-one 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。 既然处理的是一个以字符串表示的二进制数字,那么我们就按照规则和需求进行处理。最终的目的是把这个二进制数变成1,所以我们从右...
LeetCode 1342. 将数字变成 0 的操作次数 Number of Steps to Reduce a Number to Zero Table of Contents 中文版: 英文版: My answer: 解题报告: 中文版: 给你一个非负整数 num ,请你返回将它变成 0 所需要的步数。 如果当前数字是偶数,你需要把它除以 2 ;否则,减去 1 。
Number of Ways to Select Buildings 39 -- 28:32 App 【LeetCode】2400. Number of Ways to Reach a Position After Exactly k Steps 106 -- 22:39 App 【LeetCode】2025. Maximum Number of Ways to Partition an Array 177 1 23:45 App 【LeetCode】233. Number of Digit One 166 1 57:58 ...
【leetcode】1269. Number of Ways to Stay in the Same Place After Some Steps 2019-12-15 09:17 −题目如下: You have a pointer at index 0 in an array of size arrLen. At each step, you can move 1 position to the left, 1 position to the r... ...
1269 Number of Ways to Stay in the Same Place After Some Steps 停在原地的方案数 Description: You have a pointer at index 0 in an array of size arrLen. At each step, you can move 1 position to the left, 1 position to the right in the array, or stay in the same place (The poin...
【leetcode】1269. Number of Ways to Stay in the Same Place After Some Steps 2019-12-15 09:17 −题目如下: You have a pointer at index 0 in an array of size arrLen. At each step, you can move 1 position to the left, 1 position to the r... ...