【LeetCode】Number Complement 解题报告

LeetCode --- 476. Number Complement 解题报告 Given a positive integer, output its complement number. The complement strategy is to flip the bits of its binary representation. Note: The given integer is guaranteed to fit within the range of a 3... 阅读详情

Question

Given a positive integer, output its complement number. The complement strategy is to flip the bits of its binary representation.

Note

  • The given integer is guaranteed to fit within the range of a 32-bit signed integer.
  • You could assume no leading zero bit in the integer’s binary representation.

Example

Input: 5
Output: 2
Explanation: The binary representation of 5 is 101 (no leading zero bits), and its complement is 010. So you need to output 2.

Input: 1
Output: 0
Explanation: The binary representation of 1 is 1 (no leading zero bits), and its complement is 0. So you need to output 0.

Ways

找出一个二进制数的位反后的数。

本来想的是直接取反,也就是~num,但是试验下发现不行,因为有前面的0,取反后变成了1,然后就是个负数。

所以正确的方法应该是再与一下跟当前的数字的位数相同的全1的数字。其方法就是找出这个数字的第一个1,把后面的那些数字全部设为1.

没想到java竟然有这个库函数,那么就方便的多。

当然还有另外的方法,比如转成字符串,以此遍历到第一个1.

public class Solution {
    public int findComplement(int num) {
        return ~num & ((Integer.highestOneBit(num) << 1) - 1);
    }
}
Ollama最新快速上手指南:从安装到精通本地AI模型部署 想让Ollama跑你的"私房模型"?三种主流格式全支持,总有一款适合你!GGUF格式(推荐新手首选)创建Modelfile文件:执行创建命令:运行你的专属模型:PyTorch/Safetensors格式(适合进阶玩家)先用库测试模型能否正常加载确保模型结构完整后再导入Ollama官方文档有详细转换指南专业小贴士GGUF格式最轻量,PyTorch最灵活,Safetensors最安全遇到问题?试试用llama.cpp进行格式转换7B参数模型推荐使用q4_0量化级别想让AI用马里奥。 阅读详情

相关推荐

Altium Designer学习---如何进行SI仿真

Altium designer 如何进行SI仿真。         1、仿真电路中需要至少一块集成电路;             2、器件的IBIS模型;              3、在规则中必须设定电源网络和地网络;              4、建立SI规则约束;              5、层堆栈必须设置正确,电源平面必须连续;     建立的文件必须是一个工程,并把相应的文件放在工程目...

weixin_34050519的博客 4905

LeetCode 476 Number Complement 解题报告

题目要求 Given a positive integer, output its complement number. The complement strategy is to flip the bits of its binary representation. Note: The given integer is guaranteed to fit within the rang...

weixin_30262255的博客 139

掌控板玩板指南1:初识掌控板、扩展板、Mind+编辑器

掌控板、I/O扩展板入门

好乐无荒之地 1万+

Leetcode 476. Number Complement 补数 解题报告

1 解题思想这道题给了一个正的整数,然后需要计算一个特殊的补数,这个补数的计算方式是: 1、对于这个整数num,转换成对应的二进制表示,这个二进制表示共有x位(不高于32) 2、将这x位取反后,得到对应的10进制值就是结果这道题的难点在于: 1、因为java(或其他)里,int一般长度是32bit,直接按位取反不对,因为前面多余的0不能取反 2、如果遍历的话,开销比较大所以解题思想就是:

MebiuW的专栏 3340

利用Langchain生成合成数据的实践指南

合成数据是相对于从真实世界事件中收集的数据而言的人工生成的数据。它用于模拟真实数据,同时避免隐私泄露或遇到真实数据中的局限性。:尽管合成数据有诸多优点,但应谨慎使用,因为它可能无法捕捉真实世界的复杂性。

eahba的博客 399

从Pikachu靶场实战解析越权漏洞:原理、攻击与防御

本文通过Pikachu靶场实战详细解析越权漏洞的原理、攻击方式与防御策略。文章深入探讨水平越权和垂直越权的危害,结合代码示例展示漏洞利用过程,并提供代码层和架构层的立体化防御方案,帮助开发者有效防范这一Web安全隐形杀手。

weixin_29325007的博客 361

[Leetcode] 476. Number Complement 解题报告

题目: Given a positive integer, output its complement number. The complement strategy is to flip the bits of its binary representation. Note: The given integer is guaranteed to fit within t

魔豆(Magicbean)的博客 280

Solidworks转URDF避坑实录:从履带机器人到ROS仿真的完整配置流程

本文详细介绍了将Solidworks履带机器人模型转换为URDF格式并配置ROS仿真的完整流程,重点解析了坐标系设置、复杂关节定义、插件配置等关键步骤中的常见问题与解决方案,帮助开发者高效实现履带机器人在RVIZ和Gazebo中的精确仿真。

weixin_30693683的博客 458

LeetCode //C - 476. Number Complement

【代码】LeetCode //C - 476. Number Complement。

Made in Code 1295

关于无障碍适配的笔记

针对无障碍兼容性的笔记,针对wcg2.0/2.1

CoffeeAndIce的博客 4228

YOLOv11改进策略【注意力机制篇】| SE、CBAM、ECA、CA、Swin Transformer等注意力机制全面解析与应用

本文详细介绍了五种主流的注意力机制及其在YOLOv11中的应用。通过合理的模块选择和位置部署,注意力机制能够显著提升目标检测性能。

QQ_778132974的博客 877

[LeetCode] 476.Number Complement

[LeetCode] 476.Number Complement 题目描述 解题思路 实验代码 题目描述Given a positive integer, output its complement number. The complement strategy is to flip the bits of its binary representation.Note: 1. The given

user_name_is_so_long的博客 271

SAP 系统间 SSO 的真正价值,从 SM59 到可信 RFC 的安全设计

SAP S/4HANA系统间Single Sign-On(SSO)的核心价值在于集中管理通信身份与信任关系,而非仅减少密码输入。传统固定账号密码方式在系统规模扩大后会面临密码管理、审计追踪等挑战。通过SM59配置可信RFC目标系统,结合SSL证书或SAP Trusted System等强认证机制,能有效降低安全凭据暴露风险。SSO配置需配合授权对象、角色管理等建立清晰的信任边界,实现系统间安全通信的同时保持可审计性。这种方案特别适用于Fiori前端与S/4HANA后端等场景,通过可信RFC传递用户身份,避免

2007 年 ~ 2025 年,深耕 SAP 技术 18 年 1066

LeetCode-476. Number Complement [C++][Java]

Thecomplementof an integer is the integer you get when you flip all the0's to1's and all the1's to0's in its binary representation.

贫道绝缘子的博客 239

复分析 阿尔福斯(Ahlfors) 第三版答案 英文

答案分为十章,有部分习题没有答案,全英文复分析阿尔福斯(Ahlfors) 第三版答案

怀旧版电脑GBC模拟器

再现原版GBC掌机的界面,电脑运行连游戏界面都一样大小,很怀旧,很真实,(英文版)强烈推荐,很小的文件,很难才找到的。

【LeetCode】476.Number Complement 补数

题目 题目地址https://leetcode.com/problems/number-complement/description/ Given a positive integer, output its complement number. The complement strategy is to flip the bits of its binary representa

Fannie08的博客 742

IEM模型,地表粗糙度模拟

利用微波反演土壤水分时候,利用IEM模型可以实现对随机地表的模拟

LeetCode: 476. Number Complement

LeetCode: 476. Number Complement Given a positive integer, output its complement number. The complement strategy is to flip the bits of its binary representation. Note: The given integer is guar

qha0731的专栏 302

精雕软件(JDpaint) 5.5 绿色版

精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版

【LeetCode】1012. Complement of Base 10 Integer 解题报告(Python)

作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录题目描述题目大意解题方法日期 题目地址:https://leetcode.com/problems/complement-of-base-10-integer/ 题目描述 Every non-negative integer N has a binary representatio...

负雪明烛 1055

[LeetCode]476. Number Complement

[LeetCode]476. Number Complement题目描述思路优化前 例如数字5,使用 5 ^ 7 得到结果2,获取7的方法, num ^ mask = result 对num右移一位循环,直到num >= 1,在循环中计算mask *= 2,外层对mask减一得到mask 最终得到result第一次优化 循环中使用 mask <<= 1 mask ^= 1替换乘法代码优化前

Lcharon的博客 3243

LeetCode 476 : Number Complement

LeetCode 476 : Number Complement题目大意输入一个正数,求其二进制的补数的十进制形式。 Description Given a positive integer, output its complement number. The complement strategy is to flip the bits of its binary representation.N

Cecilia_whf的博客 485

Leetcode 476. Number Complement

【代码】Leetcode 476. Number Complement。

小白菜又菜 450

LeetCode-476. Number Complement

题目链接:476. Number Complement 题目描述:给你一个正整数,求这个正整数的二进数的值,全部取反后的整数的值。 这个正整数是一个小于32位的整数 你可以认为这个二进制数没有前导0,也就是说二进制数的第一位一定是1 这道题就没啥说的了,将给的正整数转化为二进制数,取反后再转化为整数,返回结果就ok。我的代码(很渣):class Solution(object): def f

Mlong54的专栏 531

leetcode 476. Number Complement | 476. 数字的补数(位运算)

题目 https://leetcode.com/problems/number-complement/ 题解 class Solution { public int findComplement(int num) { int i = Integer.numberOfLeadingZeros(num); return ~num << i >> i; } } 也可以参考评论区的答案: Idea: Flip all bits usin

寒泉 4万+

LeetCode 476. Number Complement

原题目:https://leetcode-cn.com/problems/number-complement/ 思路:每一位和1进行异或运算就可以了 代码: class Solution { public: int findComplement(int num) { long long c = 1; while(c<=num){ num = num ^ c; c <<= 1; ..

庾信平生最萧瑟 124

[leetcode]476. Number Complement

[leetcode]476. Number Complement Analysis ummmm~—— [ummmm~] Given a positive integer, output its complement number. The complement strategy is to flip the bits of its binary representation. ...

weixin_32135877的博客 128

codeforces-go 题解:LeetCode 476 数字的补数(Number Complement)的位运算异或掩码解法

本文基于算法竞赛模板库 codeforces-go 的题解笔记 [leetcode/weekly/128/a/476.md](https://link.gitcode.com/i/b1dcac38223a03904c9730bd4ee29878) 展开,剖析 LeetCode 476「数字的补数」这一经典位运算题目的核心难点——如何对整数的二进制表示"只取反有效位、不污染前导零",并给出 Pyth

gitblog_01094的博客 376
上一篇: LeetCode笔记:693. Binary Number with Alternating Bits
下一篇: [LeetCode] Sum of Two Integers 两数之和
binbigdata
博客等级 码龄9年 227粉丝 68原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值