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
相关推荐
罗西的思考1 小时前
【Agentic RL / 强化学习框架】Molt 设计解读
人工智能·算法·机器学习
hahaha60161 小时前
HLS高层次综合设计技巧--C++类和模板
图像处理·人工智能·算法·计算机视觉
多弗朗皮卡丘2 小时前
算法详解4:买卖股票的最佳时机系列(上)
算法
维克兜率天4 小时前
【维克】动量指标家族:RSI、ROC、CCI、Momentum全面解析
python·算法
AI情绪识别开源5 小时前
检信 AI 智能推广平台(代号:JX-Promote)
人工智能·算法·erlang
老当益壮梁奶奶6 小时前
Linux软件编程学习笔记(八):进程间通信详解(1)
linux·c语言·笔记·学习·算法
不会就选b6 小时前
算法日常・每日刷题--<BFS拓扑排序>4
算法
天真小巫6 小时前
2026.8.30总结
职场和发展
不正经学生6 小时前
C语言动态内存管理(上):堆上的自由与责任
c语言·开发语言·c++·算法·面试
伟大的车尔尼6 小时前
贪心的概念
数据结构·算法·贪心