1. Given a positive integer, output its complement number. The complement strategy is to flip the bits of its binary representation.
  2.  
  3. Note:
  4. The given integer is guaranteed to fit within the range of a 32-bit signed integer.
  5. You could assume no leading zero bit in the integers binary representation.
  6. Example 1:
  7. Input: 5
  8. Output: 2
  9. Explanation: The binary representation of 5 is 101 (no leading zero bits), and its complement is 010. So you need to output 2.
  10. Example 2:
  11. Input: 1
  12. Output: 0
  13. Explanation: The binary representation of 1 is 1 (no leading zero bits), and its complement is 0. So you need to output 0.

Better solution:

  1. public static int highestOneBit(int i)
Returns an int value with at most a single one-bit, in the position of the highest-order ("leftmost") one-bit in the specified int value. 
  1. public class Solution {
  2. public int findComplement(int num) {
  3. return ~num & ((Integer.highestOneBit(num) << 1) - 1);
  4. }
  5. }

一般方法:

  1. public class Solution {
  2. public int findComplement(int num) {
  3. int res = 0;
  4. int i = 31;
  5. while (i >= 0) {
  6. if (((num >>> i) & 1) == 1) break;
  7. i--;
  8. }
  9. while (i >= 0) {
  10. if (((num >>> i) & 1) == 0) {
  11. res |= 1<<i;
  12. }
  13. i--;
  14. }
  15. return res;
  16. }
  17. }

Leetcode: Number Complement的更多相关文章

  1. LeetCode——Number Complement

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

  2. [LeetCode] Number Complement 补数

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

  3. LeetCode#476 Number Complement - in Swift

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

  4. 【leetcode】476. Number Complement

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

  5. LeetCode_476. Number Complement

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

  6. 2016.5.15——leetcode:Number of 1 Bits ,

    leetcode:Number of 1 Bits 代码均测试通过! 1.Number of 1 Bits 本题收获: 1.Hamming weight:即二进制中1的个数 2.n &= (n ...

  7. LeetCode——Number of Boomerangs

    LeetCode--Number of Boomerangs Question Given n points in the plane that are all pairwise distinct, ...

  8. LeetCode_Easy_471:Number Complement

    LeetCode_Easy_471:Number Complement 题目描述 Given a positive integer, output its complement number. The ...

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

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

随机推荐

  1. excel数据导入mysql

    先把excel数据另存成txt文件 Load Data InFile 'D:/1.txt' Into Table `res_type_collect` fields terminated by '@‘ ...

  2. C语言中使用bool

    声明 C语言中是没有bool类型的. 使用方法 参考: https://stackoverflow.com/q/1921539.

  3. Beta(4/7)

    鐵鍋燉腯鱻 项目:小鱼记账 团队成员 项目燃尽图 冲刺情况描述 站立式会议照片 各成员情况 团队成员 学号 姓名 git地址 博客地址 031602240 许郁杨 (组长) https://githu ...

  4. PostgreSQL自学笔记:3 数据库的基本操作

    3 数据库的基本操作 3.1 创建数据库 3.1.1 使用对象浏览器创建数据库 [Server] -> PostgreSQL 9.6 -> 数据库,右击 -> 创建 通常: 数据库: ...

  5. 洛谷.3733.[HAOI2017]八纵八横(线性基 线段树分治 bitset)

    LOJ 洛谷 最基本的思路同BZOJ2115 Xor,将图中所有环的异或和插入线性基,求一下线性基中数的异或最大值. 用bitset优化一下,暴力的复杂度是\(O(\frac{qmL^2}{w})\) ...

  6. jdk1.8.0_40 +maven+tomcat7.0+mysql8.0详细安装教程

    (一)  jdk的安装 1.下载jdk推荐下载地址:http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-213 ...

  7. Qt写websocketpp服务端

    1.下载websocketpp,地址为https://github.com/zaphoyd/websocketpp,版本为0.7. 2.下载boost,地址为https://www.boost.org ...

  8. Spark on Yarn with HA

    Spark 可以放到yarn上面去跑,这个毫无疑问.当Yarn做了HA的时候,网上会告诉你基本Spark测不需做太多的关注修改,实际不然. 除了像spark.yarn开头的相关配置外,其中一个很重要的 ...

  9. js一些代码

    1判断金额正则 var reg = /(^[1-9]([0-9]+)?(\.[0-9]{1,2})?$)|(^(0){1}$)|(^[0-9]\.[0-9]([0-9])?$)/; var money ...

  10. 请求库之requests模块

    本片导航: 介绍 基于GET请求 基于POST请求 响应Response 高级用法   一.介绍 #介绍:使用requests可以模拟浏览器的请求,比起之前用到的urllib,requests模块的a ...