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. Redis数据结构之robj

    本文及后续文章,Redis版本均是v3.2.8 我们知道一个database内的这个映射关系是用一个dict来维护的.dict的key固定用一种数据结构来表达,这这数据结构就是动态字符串sds.而va ...

  2. ssh报错 WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED!

    今天登陆远程主机的时候,出现如下的报错信息 ssh 10.0.0.1 @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ @ WAR ...

  3. idea报错:Invalid bound statement (not found)

    在配置MyBatis接口映射的Mapper.xml时,提示Invalid bound statement (not found)异常,就算是接口和xml名字相同,路径相同也无法找到,在网上找到了几种解 ...

  4. h5页面

    <!DOCTYPE html><html lang="utf-8"><head> <meta charset="UTF-8&qu ...

  5. MyBatis:SQL语句中的foreach的详细介绍

    foreach 也就是遍历迭代,在SQL中通常用在 in 这个关键词的后面foreach元素的属性主要有 item,index,collection,open,separator,close. 分别代 ...

  6. Hibernate根据实体类自动创建表

    Hibernate支持自动建表,在开发阶段很方便,可以保证hbm与数据库表结构的自动同步. 如何使用呢?很简单,只要在hibernate.cfg.xml里加上如下代码 Xml代码<propert ...

  7. 变量类型-List

    教程:一:列表的创建 List(列表) 是 Python 中使用最频繁的数据类型.列表中元素的类型可以不相同,数字,字符串甚至可以包含列表(所谓嵌套)    (1)List写在方括号之间,元素用逗号隔 ...

  8. 学习java编程思想 第一章 对象导论

    一.面向对象的五个基本特性: 1.万物皆为对象.将对象视为奇特的变量,他可以存储数据,还可以要求它在自身上执行操作. 2.程序是对象的合集,他们通过发送消息告诉彼此所要做的. 3.每个对象都有自己的由 ...

  9. 电子产品使用感受之——为什么我把Apple Watch S2 升级到了 S4?

    2019.03.14 更新 最近在手表上安装了“摩拜单车”的APP,这绝对是一款使用体验加分的APP. 我每天上下班都要骑摩拜单车,但是每次掏出手机,首先FACE ID解锁屏幕,然后从上往下滑屏幕来触 ...

  10. 钉钉开发入门,微应用识别用户身份,获取用户免登授权码code,获取用户userid,获取用户详细信息

    最近有个需求,在钉钉内,点击微应用,获取用户身份,根据获取到的用户身份去企业内部的用户中心做校验,校验通过,相关子系统直接登陆; 就是在获取这个用户身份的时候,网上的资料七零八落的,找的人烦躁的很,所 ...