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. ionic 之 基本布局

    目录: 简介 Hybrid vs. Others ionic CSS框架 基本布局 布局模式 定高条块:.bar .bar : 位置 .bar : 嵌入子元素 .bar : 嵌入input 内容:.c ...

  2. 洛谷——P1262 间谍网络

    P1262 间谍网络 题目描述 由于外国间谍的大量渗入,国家安全正处于高度的危机之中.如果A间谍手中掌握着关于B间谍的犯罪证据,则称A可以揭发B.有些间谍收受贿赂,只要给他们一定数量的美元,他们就愿意 ...

  3. P1003 铺地毯(noip 2011)

    洛谷——P1003 铺地毯 题目描述 为了准备一个独特的颁奖典礼,组织者在会场的一片矩形区域(可看做是平面直角坐标系的第一象限)铺上一些矩形地毯.一共有 n 张地毯,编号从 1 到n .现在将这些地毯 ...

  4. java基础语法4--封装,继承,多态

    学习路线: 未封装==>封装==>继承==>多态==>抽象类 首先还是那句话,万物皆对象,对象有行为和属性. 一:封装 1.封装的概念: 信息隐蔽和对象的属性及操作结合成一个独 ...

  5. java.lang.ClassNotFoundException: org.springframework.web.servlet.DispatcherServlet

    新建Maven 项目的时候报错: java.lang.ClassNotFoundException: org.springframework.web.servlet.DispatcherServlet ...

  6. 框架-Jquerychange事件数值计算

    //优惠率计算优惠价            $("body").on("change", "#Rate", function() {     ...

  7. Linux驱动开发:USB驱动之usb_skel分析

    在学习了这么些天的驱动之后,个人觉得驱动就是个架构的问题,只要把架构弄清楚了 然后往里面添砖加瓦就可以了,所以似乎看起来不是太困难,但也许是是我经验不足吧,这只能算是个人浅见了 这两天在学习USB驱动 ...

  8. 搜索引擎keyword智能提示的一种实现

    问题背景 搜索关键字智能提示是一个搜索应用的标配.主要作用是避免用户输入错误的搜索词,并将用户引导到相应的关键词上,以提升用户搜索体验. 美团CRM系统中存在数以百万计的商家,为了让用户高速查找到目标 ...

  9. Deepin-安装php

    点击即可:下载:PHP5.6 下载完后执行下面这条shell命令即可完成安装 文件保存为:php_install.sh ,运行时:sh php_install.sh .tar.bz2 cd php- ...

  10. springMVC中的中心控制Servlet是那个类?(B)

    A:ActionServlet B:DispatcherServlet C:AbstractController D:FacesServlet