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:

  1. Input: 5
  2. Output: 2
  3. 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:

  1. Input: 1
  2. Output: 0
  3. Explanation: The binary representation of 1 is 1 (no leading zero bits), and its complement is 0. So you need to output 0.

 题目标签:Bit Manipulation
  这道题目给了我们一个int数,让我们找到它的补数。而且这个数字是正数,那么我们只要把除了leading zeros的数字都反转一下就可以了。设一个32次的for loop, 利用 & 1 来判断最后边的bit 是不是0, 如果是0,那么我们需要反转它,设一个x,规律是1,2,4,8。。。2进制, 如果是0,那么就加x,如果是1,那么不需要加。然后利用 >> 1来移动num 向右一位。 当num == 1的时候,意味着所有1左边的数字都是0了,这个时候已经不需要在继续反转下去了。
这道题目看上去挺简单的,但是一下子还没做出来。。。看来今天状态不佳,适合出去玩。
 

Java Solution:

Runtime beats 61.45%

完成日期:06/29/2017

关键词:Bit Manipulation

关键点:利用 & 1 判断最右边的bit; 利用 >> 来移动bits;判断num == 1 来终止反转

  1. public class Solution
  2. {
  3. public int findComplement(int num)
  4. {
  5. int res = 0;
  6. int x = 1;
  7.  
  8. for(int i=0; i<32; i++)
  9. {
  10. if((num & 1) == 0) // meaning num's bit is 0, need to flip this bit.
  11. res += x;
  12.  
  13. x = x * 2; // increase the x
  14.  
  15. if(num == 1) // if current num is == 1, meaning all the leading numbers are zeros; stop
  16. break;
  17.  
  18. num = num >> 1; // shift num to right by 1 bit.
  19.  
  20. }
  21.  
  22. return res;
  23. }
  24. }

参考资料:

http://www.cnblogs.com/grandyang/p/6275742.html

LeetCode 算法题目列表 - LeetCode Algorithms Questions List

LeetCode 476. Number Complement (数的补数)的更多相关文章

  1. LeetCode#476 Number Complement - in Swift

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

  2. LeetCode 476. Number Complement

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

  3. LeetCode 476 Number Complement 解题报告

    题目要求 Given a positive integer, output its complement number. The complement strategy is to flip the ...

  4. 476 Number Complement 数字的补数

    给定一个正整数,输出它的补数.补数是对该数的二进制表示取反.注意:    给定的整数保证在32位带符号整数的范围内.    你可以假定二进制数不包含前导零位.示例 1:输入: 5输出: 2解释: 5的 ...

  5. LeetCode: 476 Number Complement(easy)

    题目: Given a positive integer, output its complement number. The complement strategy is to flip the b ...

  6. 【leetcode】476. Number Complement

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

  7. 476. Number Complement(补数)

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

  8. 【LeetCode】476. Number Complement (java实现)

    原题链接 https://leetcode.com/problems/number-complement/ 原题 Given a positive integer, output its comple ...

  9. [LeetCode&Python] Problem 476. Number Complement

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

随机推荐

  1. MySQL数据库引擎介绍、区别

    数据库引擎介绍 MySQL数据库引擎取决于MySQL在安装的时候是如何被编译的.要添加一个新的引擎,就必须重新编译MYSQL.在缺省情况下,MYSQL支持三个引擎:ISAM.MYISAM和HEAP.另 ...

  2. python 浅析对return的理解

    最近很忙,但是还是很认真的学习python这个东西,不是出于什么目的,只是单纯的喜欢罢了.最近学习的东西比较简单,但是也遇到了一些问题,就是比较迷惑人的问题,今天小编就在这里讲讲自己的对return的 ...

  3. Class类与Java反射

    1反射机制是什么 反射机制是在运行状态中,对于任意一个类,都能够知道这个类的所有属性和方法:对于任意一个对象,都能够调用它的任意一个方法和属性:这种动态获取的信息以及动态调用对象的方法的功能称为jav ...

  4. CacheConcurrencyStrategy五种缓存方式

    CacheConcurrencyStrategy有五种缓存方式:  CacheConcurrencyStrategy.NONE,不适用,默认  CacheConcurrencyStrategy.REA ...

  5. ASP.Net开发WebAPI跨域访问(CORS)的精简流程

    1: Web.config里有一行: <remove name="OPTIONSVerbHandler" /> 这个要删除. 2: nuget安装Microsoft.A ...

  6. Linux配置SSH端口以及密钥登录

    改端口后重启: vim /etc/ssh/sshd_config systemctl restart sshd

  7. hadoop2.0的数据副本存放策略

    在hadoop2.0中,datanode数据副本存放磁盘选择策略有两种方式: 第一种是沿用hadoop1.0的磁盘目录轮询方式,实现类:RoundRobinVolumeChoosingPolicy.j ...

  8. this的四种绑定形式

    一 , this的默认绑定 当一个函数没有明确的调用对象的时候,也就是单纯作为独立函数调用的时候,将对函数的this使用默认绑定:绑定到全局的window对象. 一个例子 function fire ...

  9. .NET Excel导出方法及其常见问题详解

    摘要:.NET Excel导出方法及其常见问题详解. 一.Excel导出的实现方法 在.net 程序开发中,对于Excel文件的导出我们一共有三种导出方式: 利用文件输出流进行读写操作 这种方式的导出 ...

  10. Vue.js的从入门到放弃进击录(二)

    哇塞,昨晚更新的篇(一)这么多阅读量,看来入坑的人越来越多啦~熬了一个礼拜夜,今天终于生病惹~国庆要肥家咯·所以把篇(二)也更完.希望各位入坑的小伙伴能少跳几个坑呗.如果有什么不对的地方也欢迎讨论指正 ...