Leetcode 3370. Smallest Number With All Set Bits

Problem

You are given a positive number n.

Return the smallest number x greater than or equal to n, such that the binary representation of x contains only set bits

Algorithm

Using bit operations, add one bit to the left each time until a number not less than n is found.

Code

python3 复制代码
class Solution:
    def smallestNumber(self, n: int) -> int:
        ans, bits = 1, 2
        while ans < n:
            ans += bits
            bits <<= 1
        
        return ans
相关推荐
信奥卷王28 分钟前
[GESP202503 六级] 环线(含视频)
算法
CQU_JIAKE42 分钟前
10.15【A】
算法
晴天的雨.99244 分钟前
【C++】模板进阶
开发语言·c++·算法
木井巳1 小时前
【动态规划】三步问题
java·算法·leetcode·动态规划·推荐算法
楚识科技1 小时前
手写 OCR 识别技术深度解析:从算法原理到政企级落地实现
算法·ocr
leobertlan10 小时前
痛苦系列 | DSP-03 跨越单位圆:Z变换与零极点分析
算法·程序员
蛋蛋的就会顺顺的10 小时前
hot100 p0——栈,贪心算法,动态规划
java·前端·javascript·算法·leetcode·力扣
开发小能手12311 小时前
答案daanC语言
数据结构·算法
小 黄 鸡11 小时前
HashMap 面试必会知识点详解(JDK 8)
java·面试·职场和发展
Navigator_Z12 小时前
LeetCode //C - 1291. Sequential Digits
c语言·算法·leetcode