1 解题思想

这道题给了一个正的整数,然后需要计算一个特殊的补数,这个补数的计算方式是:
1、对于这个整数num,转换成对应的二进制表示,这个二进制表示共有x位(不高于32)
2、将这x位取反后,得到对应的10进制值就是结果

这道题的难点在于:
1、因为java(或其他)里,int一般长度是32bit,直接按位取反不对,因为前面多余的0不能取反
2、如果遍历的话,开销比较大

所以解题思想就是:
1、利用java里的方法Integer.highestOneBit,获得数字num出现1的最高位(也就是对应的2进制里,第一个出现1的位为1,其他为0的值)
2、将上面的值左移2,然后减1,就可以构造一个正好覆盖num整个数x位长度的遮罩mask(后x位为1,其他为0)
3、使用如上mask,将num的部分进行取反就可以

2 原题

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 1:
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.
Example 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.

3 AC解

public class Solution {
// 制作一个mask,在mask的范围内按位取反public int findComplement(int num) {//System.out.println("binary bits of num:"+Integer.toBinaryString(num));//System.out.println("binary bits of HigestOneBit of num:"+Integer.toBinaryString(Integer.highestOneBit(num)));int mask = (Integer.highestOneBit(num) << 1) -1;//System.out.println("binary bits of mask:"+Integer.toBinaryString(mask));num = ~num;return num & mask;}
}

Leetcode 476. Number Complement 补数 解题报告相关推荐

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

    题目 https://leetcode.com/problems/number-complement/ 题解 class Solution {public int findComplement(int ...

  2. LeetCode 476. Number Complement

    题目: Given a positive integer, output its complement number. The complement strategy is to flip the b ...

  3. LeetCode 167.Two Sum II 解题报告

    LeetCode 167.Two Sum II 解题报告 题目描述 Given an array of integers that is already sorted in ascending ord ...

  4. 【LeetCode】91. Decode Ways 解题报告(Python)

    [LeetCode]91. Decode Ways 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fux ...

  5. 263.Ugly Number||202 happy number||476 Number Complement||136 Single Number

    263.Ugly Number 判断因数是否只有素数2.3.5.. 感觉比较简单: class Solution(object):def isUgly(self, num):""& ...

  6. [LeetCode]844. Backspace String Compare 解题报告(C++)

    [LeetCode]844. Backspace String Compare 解题报告(C++) 题目描述 Given two strings S and T, return if they are ...

  7. Java实现 LeetCode 476 数字的补数

    476. 数字的补数 给定一个正整数,输出它的补数.补数是对该数的二进制表示取反. 示例 1: 输入: 5 输出: 2 解释: 5 的二进制表示为 101(没有前导零位),其补数为 010.所以你需要 ...

  8. leetcode 476. 数字的补数(Number Complement)

    目录 题目描述: 示例 1: 示例 2: 解法: 题目描述: 给定一个正整数,输出它的补数.补数是对该数的二进制表示取反. 注意: 给定的整数保证在32位带符号整数的范围内. 你可以假定二进制数不包含 ...

  9. leetcode 476. 数字的补数(Java版)| How to extract ‘k’ bits from a given position in a number

    题目 https://leetcode-cn.com/problems/number-complement/ 思路 我们想要返回已知数字的补数(num>=1). 思路: 获取 num 的二进制数 ...

最新文章

  1. 【廖雪峰Python学习笔记】错误、调试、测试
  2. 关于VS中区分debug与release,32位与64位编译的宏定义
  3. python爬虫教程入门-零基础入门Python爬虫不知道怎么学?这是入门的完整教程
  4. FastReport的模板文件frx文件啊怎样打开并编辑修改
  5. .NET Core with 微服务 - Consul 配置中心
  6. C++程序设计(第2版)课后习题答案--第11章
  7. 【转载】C++读写ini配置文件GetPrivateProfileString()WritePrivateProfileString()
  8. oc代码混淆_OC代码混淆工具
  9. BeanFactory和FactoryBean区别
  10. [iOS_Dev] 官方Mac OS X.dmg 下载,dmg 转 iso,Mac 镜像。
  11. vba手机号码归属_Android中手机号码归属地查询实现
  12. 关于数据库、数据仓库、数据湖、数据中台概念和区别
  13. ISAPI_Rewrite
  14. 图像表示的相关概念:图像深度、像素深度、位深的区别和关系
  15. dedecms后台界面更改
  16. Jquery Mobile dialog的生命周期 - 落叶潇潇雨 - 博客园
  17. 如何管理一盘散沙的团队?
  18. 花生棒虚拟服务器,花生棒 开服务器
  19. 简约生活的72条观念
  20. 用wxpython编写登录界面_用wxPython打造Python图形界面(上)

热门文章

  1. “水仙花数”与变种“水仙花数”
  2. 一、Storm是什么?
  3. 全球隔离,生出不少坏毛病
  4. 【全网最全最细】青龙面板搭配Ninja+依赖+Ninja配置的超细讲解教程!!!
  5. Hyper-V (window 10 家庭版安装 Hyper-V)
  6. C/C++中的绝对值函数
  7. 【启明云端】启明云端带你揭开WT32-S3-WROVER神秘面纱
  8. Educational Codeforces Round 88 (Rated for Div. 2) C. Mixing Water (思维,数学)
  9. 微服务学习总结5(Ocelot+Polly+Consul)
  10. UI层自动化测试介绍