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
相关推荐
独自破碎E14 分钟前
【前缀和+哈希】LCR_011_连续数组
算法·哈希算法
一条大祥脚21 分钟前
26.1.26 扫描线+数论|因子反演+子序列计数|树套树优化最短路
数据结构·算法
m0_5613596724 分钟前
基于C++的机器学习库开发
开发语言·c++·算法
星空露珠31 分钟前
速算24点所有题库公式
开发语言·数据库·算法·游戏·lua
2401_8324027532 分钟前
C++中的类型擦除技术
开发语言·c++·算法
努力学习的小廉40 分钟前
我爱学算法之—— 递归回溯综合(二)
开发语言·算法
sheji526142 分钟前
JSP基于信息安全的读书网站79f9s--程序+源码+数据库+调试部署+开发环境
java·开发语言·数据库·算法
2301_7634724642 分钟前
C++网络编程(Boost.Asio)
开发语言·c++·算法
依依yyy1 小时前
沪深300指数收益率波动性分析与预测——基于ARMA-GARCH模型
人工智能·算法·机器学习
踩坑记录1 小时前
leetcode hot100 23. 合并 K 个升序链表 hard 分治 迭代
leetcode·链表