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
相关推荐
ai产品老杨3 分钟前
国产NPU视觉算法完整流程
算法
过期的秋刀鱼!6 分钟前
带替换的采样
人工智能·python·算法·决策树·机器学习
过期的秋刀鱼!24 分钟前
使用多个决策树
人工智能·算法·决策树·机器学习·数据挖掘
203号居民1 小时前
LeetCode hot 100 —560. 和为 K 的子数组
java·算法·leetcode
退休倒计时2 小时前
【每日一题】LeetCode 20. 有效的括号 TypeScript
算法·leetcode·职场和发展·typescript
文心快码BaiduComate2 小时前
光本位联合文心快码打造面向光电芯片研发的全栈AI Agent Lightmate
算法
一次旅行2 小时前
ViT/CLIP/LLaVA/GPT-4V/VideoLLM多模态架构全解:原理仿真+工业落地选型+完整推理代码
人工智能·算法·架构
神明不懂浪漫3 小时前
【第五章】队列
开发语言·数据结构·经验分享·笔记·算法
happy_baymax3 小时前
MATLAB MCP Server+ MATLAB Agentic Toolkit+Simulink Agentic Toolkit自动更新批处理文件.ps1
人工智能·算法