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
相关推荐
csdn_aspnet24 分钟前
Python 算法快闪 LeetCode 编号 70 - 爬楼梯
python·算法·leetcode·职场和发展
一只机电自动化菜鸟2 小时前
一建机电备考笔记(40) 建筑机电施工—排水管道施工(含考频+题型)
经验分享·笔记·学习·职场和发展·课程设计
m0_629494733 小时前
LeetCode 热题 100-----26.环形链表 II
数据结构·算法·leetcode·链表
壹号用户3 小时前
用队列实现栈
数据结构·算法
做人求其滴4 小时前
面试经典 150 题 380 274
c++·算法·面试·职场和发展·力扣
daad7774 小时前
记一组无人机IMU传感器数据
算法
计算机安禾4 小时前
【c++面向对象编程】第42篇:模板特化与偏特化:为特定类型定制实现
开发语言·c++·算法
小卡不对头4 小时前
软考中级通过率怎样?软考中级哪个通过率高
职场和发展·产品经理
小O的算法实验室4 小时前
2026年KBS,流形感知强化学习差分进化算法+不规则3D无人机路径规划,深度解析+性能实测
算法·智能算法·智能算法改进