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
相关推荐
WolfGang00732113 分钟前
代码随想录算法训练营 Day50 | 图论 part08
数据结构·算法·图论
aini_lovee2 小时前
多目标粒子群优化(MOPSO)双适应度函数MATLAB实现
人工智能·算法·matlab
yong99902 小时前
图像融合与拼接:完整MATLAB工具箱
算法·计算机视觉·matlab
春风不语5052 小时前
深入理解主成分分析(PCA)
算法
apollowing2 小时前
启发式算法WebApp实验室:从搜索策略到群体智能的能力进阶(二十二)
算法·启发式算法·web app
晚枫歌F2 小时前
最小堆定时器
数据结构·算法
Lumos_7773 小时前
Linux -- 线程
java·jvm·算法
七颗糖很甜3 小时前
“十五五”气象发展规划:聚焦五大核心任务
大数据·python·算法
科研前沿4 小时前
镜像视界浙江科技有限公司的关键技术突破有哪些?
大数据·人工智能·科技·算法·音视频·空间计算
Fuly10244 小时前
技术经理面试相关--技术篇
面试·职场和发展