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. CF613A:Peter and Snow Blower

    用一个圆心在(x,y)的圆环覆盖一个n边形,顺或逆时针给出n边形所有顶点,求圆环最小面积. 卡了好久,各种傻逼错误.. 题目就是让我们固定一大一小两个边界圆,我们来看看这两个圆满足什么条件. 首先外面 ...

  2. 【zTree】zTree根据后台数据生成树并动态设置前面的节点复选框的选中状态

    0.页面中准备树的ul <ul id="treeDemo10" class="ztree" style="display: none;" ...

  3. CodeForces 593A 2Char

    暴力. #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> u ...

  4. Copy List with Random Pointer (Hash表)

    A linked list is given such that each node contains an additional random pointer which could point t ...

  5. HDU 1244 【DP】

    题意: 中文. 思路: 先初步处理,用give-take求出每个城市剩的钱. 求解问题转化成使得和不小于0的最长连续字串. 枚举起点,然后当该起点加的和为负时开始枚举下一起点.(这个状态的转移) 2W ...

  6. 从CLR GC到CoreCLR GC看.NET Core对云原生的支持

    内存分配概要 前段时间在园子里看到有人提到了GC学习的重要性,很赞同他的观点.充分了解GC可以帮助我们更好的认识.NET的设计以及为何在云原生开发中.NET Core会占有更大的优势,这也是一个程序员 ...

  7. 【effective c++】模板与泛型编程

    模板元编程:在c++编译器内执行并于编译完成时停止执行 1.了解隐式接口和编译期多态 面向对象编程总是以显式接口(由函数名称.参数类型和返回类型构成)和运行期多态(虚函数)解决问题 模板及泛型编程:对 ...

  8. HTC 328T 如何恢复出厂设置

    设置-存储-恢复出厂设置(在存储的最下面,往下拉)

  9. Redis 入门指南

    就是DBIdx

  10. activiti自己定义流程之自己定义表单(二):创建表单

    注:环境配置:activiti自己定义流程之自己定义表单(一):环境配置 在上一节自己定义表单环境搭建好以后,我就正式開始尝试自己创建表单,在后台的处理就比較常规,主要是针对ueditor插件的功能在 ...