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. iOS swift 代理协议

    swift中的代理实现和oc中是有区别的 protocol HXQLimitedTextFieldDelegate{ func test() } 代理中默认所有方法都是required,如果需要某个代 ...

  2. python如何连接mysql数据库

    先花点时间来说说一个程序怎么和数据库进行交互1.和数据库建立连接2.执行sql语句,接收返回值3.关闭数据库连接使用MySQLdb也要遵循上面的几步.让我们一步步的进行. 1.MySQL数据库要用My ...

  3. 一个简单的servlet容器

    [0]README 0.1)本文部分文字转自 “深入剖析Tomcat”,旨在学习  一个简单的servlet容器  的基础知识: 0.2)for complete source code, pleas ...

  4. day6笔记

    一.上节回顾 list:li = [1,2,3,5,'a']增加:append:末尾加入==追加 insert:插入,在任意位置,insert(index,'内容') extend:迭代着加入,'as ...

  5. 单线程爬虫VS多线程爬虫的效率对比

    单线程爬虫: import re import requests import time url_EB = 'http://www.amazon.com/gp/search/other/ref=sr_ ...

  6. array_sum的用法

    众所周知,PHP中函数是功能很强大的,那么今天就说下array_sum的功能吧. 函数功能:返回数组中所有值的和. 举例: <?php $a = array(1,2); $b = array_s ...

  7. spring-security+hibernate4+quartz实现的心跳检测项目(转 收藏)

    转自:http://www.52itstyle.com/thread-27470-1-1.html HeartBeat. ~2 Z8 U! ?8 r心跳检测各类应用服务器(如Tomcat,Jetty) ...

  8. iOS提交到appstore的新要求

    本文转载至http://blog.csdn.net/kqygww/article/details/41277555     64-bit and iOS 8 Requirements for New ...

  9. shell脚本中格式化日期

    date [-u] [-d datestr] [-s datestr] [--utc] [--universal] [--date=datestr] [--set=datestr] [--help] ...

  10. nginx1.4.7+uwsgi+django1.9.2项目部署,liunx系统为ubuntu14.0.4。

    本文基于root用户下进行部署,django项目名称为BDFS 1.  安装依赖包,终端输入命令 1)         环境依赖包 apt-get update apt-get install pyt ...