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. HDU 3763 CDs

    http://acm.hdu.edu.cn/showproblem.php?pid=3763 题意: 两组数据 看重复的有多少 如果每输入一个就去查找的话O(n^2) 会超时 所以可以用二法 第一组数 ...

  2. Codeforces936C. Lock Puzzle

    给个串,只能用操作shift x表示把后面x个字符翻转后放到串的前面.问s串怎么操作能变t串.n<=2000,操作次数<=6100. 打VP时这转来转去的有点晕... 可以想一种逐步构造的 ...

  3. UOJ 58 (树上带修改的莫队)

    UOJ 58 糖果公园 Problem : 给一棵n个点的树,每个点上有一种颜色,对于一条路径上的点,若 i 颜色第 j 次出现对该路径权值的贡献为 w[i] * c[j], 每次询问一条路径的权值, ...

  4. Codeforces 645D Robot Rapping Results Report【拓扑排序+二分】

    题目链接: http://codeforces.com/problemset/problem/645/D 题意: 给定n个机器人的m个能力大小关系,问你至少要前几个大小关系就可以得到所有机器人的能力顺 ...

  5. c:forEach varStatus 属性

    c:forEach varStatus 属性 current: 当前这次迭代的(集合中的)项 index: 当前这次迭代从 0 开始的迭代索引 count: 当前这次迭代从 1 开始的迭代计数 fir ...

  6. P2384 最短路 洛谷

    https://www.luogu.org/problem/show?pid=2384 题目背景 狗哥做烂了最短路,突然机智的考了Bosh一道,没想到把Bosh考住了...你能帮Bosh解决吗? 他会 ...

  7. 【网络】TCP的流量控制

    一.利用滑动窗口实现流量控制 流量控制是让发送方的发生速率不要太快,要让接收方来得及接收. 发送方的发送窗口不能超过接收方给出的接收窗口的数值,TCP的窗口单位是字节,不是报文段. TCP为每一个连接 ...

  8. 云上领跑,快人一步:华为云抢先发布Redis5.0

    12月17日,华为云在DCS2.0的基础上,快人一步,抢先推出了新的Redis 5.0产品,这是一个崭新的突破.目前国内在缓存领域的发展普遍停留在Redis4.0阶段,华为云率先发布了Redis5.0 ...

  9. CodeForces 567C. Geometric Progression(map 数学啊)

    题目链接:http://codeforces.com/problemset/problem/567/C C. Geometric Progression time limit per test 1 s ...

  10. STL_算法_元素计数(count、count_if)

    C++ Primer 学习中.. . 简单记录下我的学习过程 (代码为主) count . count_if #include<iostream> #include<cstdio&g ...