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
相关推荐
fie88891 小时前
NSCT(非下采样轮廓波变换)的分解和重建程序
算法
晨晖22 小时前
单链表逆转,c语言
c语言·数据结构·算法
沐雪架构师2 小时前
大模型Agent面试精选15题(第四辑)-Agent与RAG(检索增强生成)结合的高频面试题
面试·职场和发展
YoungHong19922 小时前
面试经典150题[072]:从前序与中序遍历序列构造二叉树(LeetCode 105)
leetcode·面试·职场和发展
im_AMBER3 小时前
Leetcode 78 识别数组中的最大异常值 | 镜像对之间最小绝对距离
笔记·学习·算法·leetcode
鼾声鼾语4 小时前
matlab的ros2发布的消息,局域网内其他设备收不到情况吗?但是matlab可以订阅其他局域网的ros2发布的消息(问题总结)
开发语言·人工智能·深度学习·算法·matlab·isaaclab
LYFlied4 小时前
【每日算法】LeetCode 25. K 个一组翻转链表
算法·leetcode·链表
Swizard4 小时前
别再迷信“准确率”了!一文读懂 AI 图像分割的黄金标尺 —— Dice 系数
python·算法·训练
s09071364 小时前
紧凑型3D成像声纳实现路径
算法·3d·声呐·前视多波束
可爱的小小小狼4 小时前
算法:二叉树遍历
算法