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. 代码生成工具更新--快速生成Winform框架的界面项目

    在之前版本的代码生成工具Database2Sharp中,由于代码生成都是考虑Winform和Web通用的目的,因此Winform界面或者Web界面都是单独生成的,在工具中生成相应的界面后,复制到项目里 ...

  2. Python学习(三十六)—— Cookie、Session和自定义分页

    一.Django中操作Cookie 获取Cookie request.COOKIES['key'] request.get_signed_cookie(key, default=RAISE_ERROR ...

  3. mybatis循环、mybatis传map

    mybatis中使用循环.mybatis传入map案例 <!-- 根据id修改商户提成配置--> <update id="editStopAll" paramet ...

  4. Prime Distance POJ - 2689 (数学 素数)

    The branch of mathematics called number theory is about properties of numbers. One of the areas that ...

  5. ORA-12541:tns:no listener

    打开任务管理器,查看服务,看箭头所指的三个服务是否开启即可

  6. Windows应用程序组成及编程步骤

    Windows应用程序组成及编程步骤: 1.应用程序的组成:一个完整的Windows应用程序通常由五种类型的文件组成 1.C语言源程序文件 2.头文件 3.模块定义文件 4.资源描述文件 5.项目文件 ...

  7. URL 链接中 井号#、问号?、连接符& 分别有什么作用?

    在一个 URL 中可以包含很多的内容,其中不仅仅是包含 26 个英文字母,10 个罗马数字,中文汉字,还可以拥有井号“#”.问号“?”.连接符“&”等三种最常见的符号,那么这些符号在网站中都有 ...

  8. [JAVA] TicTacToe实现Socket通信(一)

    先来两张预览,大家可以试试jar包了,有什么问题评论哈,过两天贴代码 jar包这里下载 https://github.com/Andy-ZYA/TicTacToe_JAVA_Socket_Swing

  9. NTSC PAL 介绍

    NTSC-J是日本地区的模拟 电视系统和视频显示标准,于2011年7月24日在全国47个县中的44个地区停止运营.模拟广播于2012年3月31日在2011年Tōhoku摧毁的三个县停止地震和海啸(岩手 ...

  10. 使用C#.NET列举组合数前N项和

    列举如下列所示的组合数前N项和,代码如下(递归方法里注意去重): static void Main(string[] args) { List<).ToList(); File.AppendAl ...