LeetCode_Easy_471: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 integer is guaranteed to fit within the range of a 32-bit signed integer.
  2. 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.

思路笔记

高效解

我能想到的直观思路,就是在二进制转十进制的时候将各位余数数值取反,然后再转换为十进制。

但是知道我看到了最优解:Java 1 line bit manipulation solution(仅仅一句话代码)

    public int findComplement(int num) {
return ~num & ((Integer.highestOneBit(num) << 1) - 1);
}

收获:与非位运算

我们首先要理解位运算:

说明:

 位运算包括:“与”、“非”、“或”、“异或”。
运算符主要针对两个二进制数的位进行逻辑运算

非:

    非运算符用符号“~”表示,其运算规律如下:

  如果位为0,结果是1,如果位为1,结果是0,也就是每位都取反了。

public static void main(String[] args)
{
int a=2;
System.out.println("a 非的结果是:"+(~a));
}

在上述代码中:结果是 10 —> 01

与:

与运算符用符号“&”表示,其使用规律如下:

两个操作数中位都为1,结果才为1,否则结果为0

public static void main(String[] args)
{
int a=129;
int b=128;
System.out.println("a 和b 与的结果是:"+(a&b));
}

在上述代码中:结果是 100101001&100101000—>100101000

 然后我们总结一下一句话代码:

  1.     假设Num=5(101),我们求出其非值为(11111111111111111111111111111010)这是一个32位的值,可是我们只想要其后三位。
  2. Integer.highestOneBit(num) << 1) - 1,可以快速求出和其长度相同的且各位为1的值,即111。
  3. 两者求与,剩下的就是010。

LeetCode_Easy_471:Number Complement的更多相关文章

  1. 【leetcode】476. Number Complement

    problem 476. Number Complement solution1: class Solution { public: int findComplement(int num) { //正 ...

  2. LeetCode——Number Complement

    LeetCode--Number Complement Question Given a positive integer, output its complement number. The com ...

  3. LeetCode_476. Number Complement

    476. Number Complement Easy Given a positive integer, output its complement number. The complement s ...

  4. LeetCode#476 Number Complement - in Swift

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

  5. LeetCode 476. Number Complement

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

  6. 476. Number Complement

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

  7. Number Complement

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

  8. LeetCode 476. Number Complement (数的补数)

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

  9. 【LeetCode】476. Number Complement (java实现)

    原题链接 https://leetcode.com/problems/number-complement/ 原题 Given a positive integer, output its comple ...

随机推荐

  1. Google Chrome v48.0.2564.

    http://www.pcpop.com/doc/1/1819/1819996.shtml 摘要:谷歌浏览器Chrome Stable 稳定版迎来例行升级,新版本号为v48.0.2564.82,上一版 ...

  2. 深入浅出--iOS的TCP/IP协议族剖析&&Socket

    深入浅出--iOS的TCP/IP协议族剖析&&Socket   简介 该篇文章主要回顾--TCP/IP协议族中的TCP/UDP.HTTP:还有Socket.(--该文很干,酝酿了许久! ...

  3. redis中毒

    黑客用我们服务器挖矿了 新的一天的开始 周五早上刚到公司,同事来问我系统为啥打不开了?我第一反应就是肯定 Nginx 服务器挂了呗,立马就去登录服务器看看,但此时发现已经完全远程登录不上这台部署了 N ...

  4. python 开发技巧(2)-- Django的安装与使用

    一.安装Django pip3 install django 或者直接使用PyCharm安装 参考 二.添加环境变量 将 "(python安装路径)\Scripts" 添加到环境变 ...

  5. Android之——JNI配置C语言打印Logcat信息

    转载请注明出处:http://blog.csdn.net/l1028386804/article/details/47425073 这篇文章给大家介绍一下在JNI中怎样为C语言配置打印Logcat信息 ...

  6. zmq重点

    The zmq_msg_send(3) method does not actually send the message to the socket connection(s). It queues ...

  7. C++ RTTI的应用

    先看下方的代码,我们所处的context在<<< void* pX = (void*)pGiven; >>>处,只知道上面这些类的信息和pX指针,怎么判断pX指向对 ...

  8. WINDOWS 7.1 SDK 安装失败

    错误提示: Please refer to Samples\Setup\HTML\ConfigDetails.htm document for further information. 原因:本机上安 ...

  9. [Spring MVC]学习笔记--FreeMarker的使用

    还是先贴出该例子存于github上的位置 https://github.com/lemonbar/spring-mvc-freemarker Sping-Framework 的官方文档简单列出了在sp ...

  10. svn 更新文件冲突,提示中文乱码解决

    问题描述: update 操作提示错误信息,中文乱码 和 “Please execute the 'Cleanup' command.” Cleanup 操作报错: 解决办法: 1. 工具下载(sql ...