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
相关推荐
我命由我123456 分钟前
Photoshop - Photoshop 工具栏(58)锐化工具
学习·ui·职场和发展·求职招聘·职场发展·学习方法·photoshop
独自破碎E7 分钟前
【队列】按之字形顺序打印二叉树
leetcode
mjhcsp9 分钟前
C++ KMP 算法:原理、实现与应用全解析
java·c++·算法·kmp
lizhongxuan10 分钟前
Manus: 上下文工程的最佳实践
算法·架构
AlenTech14 分钟前
206. 反转链表 - 力扣(LeetCode)
数据结构·leetcode·链表
踩坑记录14 分钟前
leetcode hot100 438. 找到字符串中所有字母异位词 滑动窗口 medium
leetcode·职场和发展
CS创新实验室22 分钟前
《计算机网络》深入学:海明距离与海明码
计算机网络·算法·海明距离·海明编码
WW_千谷山4_sch24 分钟前
MYOJ_10599:CSP初赛题单10:计算机网络
c++·计算机网络·算法
YuTaoShao44 分钟前
【LeetCode 每日一题】1458. 两个子序列的最大点积——(解法三)状态压缩
算法·leetcode·职场和发展
位东风1 小时前
希尔排序(Shell Sort)详解
算法·排序算法