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.

Better solution:

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. 
 public class Solution {
public int findComplement(int num) {
return ~num & ((Integer.highestOneBit(num) << 1) - 1);
}
}

一般方法:

 public class Solution {
public int findComplement(int num) {
int res = 0;
int i = 31;
while (i >= 0) {
if (((num >>> i) & 1) == 1) break;
i--;
}
while (i >= 0) {
if (((num >>> i) & 1) == 0) {
res |= 1<<i;
}
i--;
}
return res;
}
}

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. 一、自动化平台搭建-python虚拟环境安装

    主要知识点介绍: 安装django环境 创建django项目 设计模型类并利用模型类和数据库进行交互 使用django后台管理数据 编写视图函数,进行URL配置 模板的使用 图书-英雄案例完成 1.虚 ...

  2. 命令行编译C程序

    1 准备工作 下载mingw-get-setup.exe并且安装  参考 http://www.jb51.net/softjc/159871.html 环境变量更新: PATH .;C:\MinGW\ ...

  3. SpringBoot使用Sharding-JDBC分库分表

    本文介绍SpringBoot使用当当Sharding-JDBC进行分库分表. 1.有关Sharding-JDBC 有关Sharding-JDBC介绍这里就不在多说,之前Sharding-JDBC是当当 ...

  4. 【AtCoder】【模拟】【模型转化】Camel and Oases(AGC012)

    题意: 有一个骆驼,n个绿洲遍布在数轴上,第i个绿洲的坐标为x[i],保证x[i]单增.骆驼的驼峰有体积初始值V.当驼峰的体积变为v的时候,驼峰中至多只能够存储v L的水.骆驼希望走完所有的绿洲,并且 ...

  5. asp 获取url 返回值 和 对json 返回值的处理

    Function GetHttpPage(HttpUrl,endoce) If endoce = "" Then endoce = "GB2312" If Is ...

  6. SSH(Spring Struts2 Hibernate)框架整合(xml版)

    案例描述:使用SSH整合框架实现部门的添加功能 工程: Maven 数据库:Oracle 案例架构: 1.依赖jar包pom.xml <project xmlns="http://ma ...

  7. [LeetCode] Random Pick with Weight 根据权重随机取点

    Given an array w of positive integers, where w[i] describes the weight of index i, write a function  ...

  8. Wireshark简单使用教程2——附视频

    视频链接https://www.bilibili.com/video/av35336089/ 目录 对抓取的流量包进行简单的说明 Wireshark的捕获过滤器和显示过滤器 内容 1.对抓取的流量包进 ...

  9. Java语言基础之数组

    引出数组和数组的定义 为什么要使用数组: 问题一: 声明变量时,每一个单独的变量都要对应一个变量名,但现在要处理一组相同类型的数据时,如要表示班上100个人的年纪,绝不能定义100个变量来表示每一个人 ...

  10. 这样,可以在firefox播放flash了

    <object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" width="62" heigh ...