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:

    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.
  3. 思路:给定一个正整数,输出它的补数。补数的策略是通过翻转二进位表示。
  4. 1.求出该数的二进制位数; 
    2.通过给定数num和相同位数的二进制数(全为1)进行异或,即可求出补数。
  5.  public int findComplement(int num) {
    
                int ans = 0;//the count of the num's bits
    int temp = num;//copy of the num
    while(temp != 0){
    ans++;
    temp /= 2;
    }
    return num ^ (int)(Math.pow(2,ans)-1);
    }

    pow() 函数用来求 x 的 y 次幂(次方),其原型为:
             double pow(double x, double y);

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

  1. 【leetcode】476. Number Complement

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

  2. LeetCode#476 Number Complement - in Swift

    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 bits ...

  4. 476 Number Complement 数字的补数

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

  5. [LeetCode] Number Complement 补数

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

  6. LeetCode 476. Number Complement

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

  7. 476. Number Complement

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

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

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

  9. LeetCode 476 Number Complement 解题报告

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

随机推荐

  1. [NOIP1998] 提高组 洛谷P1012 拼数

    题目描述 设有n个正整数(n≤20),将它们联接成一排,组成一个最大的多位整数. 例如:n=3时,3个整数13,312,343联接成的最大整数为:34331213 又如:n=4时,4个整数7,13,4 ...

  2. 【BZOJ3295】动态逆序对(BIT套动态加点线段树)

    题意:对于序列A,它的逆序对数定义为满足i<j,且Ai>Aj的数对(i,j)的个数. 给1到n的一个排列,按照某种顺序依次删除m个元素,你的任务是在每次删除一个元素之前统计整个序列的逆序对 ...

  3. List和Map、Set的区别

    首先 List 和 Set 是存储单列数据的集合,Map 是存储键和值这样的双列数据的集合:List 中存储的数据是有顺序,并且允许重复:Map 中存储的数据是没有顺序的,其键是不能重复的,它的值是可 ...

  4. 数据库中的DDL/DML/DCL解释(转)

    DDL is Data Definition Language statements. Some examples:数据定义语言,用于定义和管理 SQL 数据库中的所有对象的语言 1.CREATE - ...

  5. Java和C++里面的重写/隐藏/覆盖

    首先,无关重载. 注:重载是同一个类的各个函数之间的.重写是父类子类之间的.Overload和Overwrite(也叫Override)的区别. 注意:Java里面区分重写(Override/Over ...

  6. TUN/TAP区别

    在计算机网络中,TUN与TAP是操作系统内核中的虚拟网络设备.不同于普通靠硬件网路板卡实现的设备,这些虚拟的网络设备全部用软件实现,并向运行于操作系统上的软件提供与硬件的网络设备完全相同的功能. TA ...

  7. 使用WIN32汇编语言实现一个基本windows窗体的过程分析

    一个常规的windows窗体一般都是一些一样的构造.你假设想要更改一些个性化的设置,你能够在这个一般的模板伤添砖加瓦.构造自己比較喜欢的类型.下边就分析一下一般的windows窗体的一般模板. 一. ...

  8. 当Eclipse爱上SVN

    推荐使用:Subclipse  :http://jingyan.baidu.com/article/1612d5007d41e9e20e1eeeff.html 为离线安装做准备: 1.下载Subver ...

  9. Find Minimum in Rotated Sorted Array 旋转数组中找最小值 @LeetCode

    O(n)的算法就不说了,这题主要考查的是 O(logn)的算法. 有序数组easy想到使用二分查找解决.这题就是在二分基础上做一些调整.数组仅仅有一次翻转,能够知道原有序递增数组被分成两部分,这俩部分 ...

  10. 搭建Maven私服(使用Nexus)

    搭建私服能够做什么? 1.假设公司开发组的开发环境所有内网.这时怎样连接到在互联网上的Maven中央仓库呢? 2.假设公司常常开发一些公共的组件.怎样共享给各个开发组.使用拷贝方式吗?假设这样,公共库 ...