476. Number Complement

Easy

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.
package leetcode.easy;

public class NumberComplement {
public int findComplement(int num) {
int complement = 0;
int pos = 0;
while (num > 0) {
if ((num & 1) == 0) {
complement |= (1 << pos);
} pos++;
num >>>= 1;
} return complement;
} @org.junit.Test
public void test() {
System.out.println(findComplement(5));
System.out.println(findComplement(1));
}
}

LeetCode_476. Number Complement的更多相关文章

  1. 【leetcode】476. Number Complement

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

  2. LeetCode——Number Complement

    LeetCode--Number Complement Question Given a positive integer, output its complement number. The com ...

  3. LeetCode_Easy_471:Number Complement

    LeetCode_Easy_471:Number Complement 题目描述 Given a positive integer, output its complement number. The ...

  4. LeetCode#476 Number Complement - in Swift

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

  5. LeetCode 476. Number Complement

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

  6. 476. Number Complement

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

  7. Number Complement

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

  8. LeetCode 476. Number Complement (数的补数)

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

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

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

随机推荐

  1. JMeter聚合报告的参数含义

    Label----每个请求的名称,比如HTTP请求等 #Samples----发给服务器的请求数量 Average----单个请求的平均响应时间 毫秒ms Median----50%请求的响应时间   ...

  2. linux下使用openssl生成https的crt和key证书

    x509证书一般会用到三类文,key,csr,crt Key 是私用密钥openssl格,通常是rsa算法. Csr 是证书请求文件,用于申请证书.在制作csr文件的时,必须使用自己的私钥来签署申,还 ...

  3. CalcIntegerLength

    private static int CalcIntegerLength(int iLen) { //String str_iLen = Convert.ToString(iLen); int str ...

  4. Linux 防火墙 | Linux 服务器如何开放端口 配置防火墙

    sudo ufw status(如果你是root,则去掉sudo,ufw status)可检查防火墙的状态,我的返回的是:inactive(默认为不活动).sudo ufw version防火墙版本: ...

  5. arduino 开发视频

    http://blog.uctronics.com/downloads/shields/ArduCAM_Camera_Shield_V2_DS.pdf http://www.arducam.com/k ...

  6. mysql和sqliet连接

    Python里Django框架数据库要配置1.setting已经自己配置好2.需要自己连接.找到setting里DATABASES进行连接自己数据库MySQL数据库连接 model里创建数据表就是Dj ...

  7. C# 请求数据 方式1

    public static string PostWebRequest2() { HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create( ...

  8. 单片机模块化程序: 单片机加入JSON是个不错的选择

    这节需要知道的知识点 https://www.cnblogs.com/yangfengwu/p/11685325.html 下载JSON文件: 链接:         https://sourcefo ...

  9. 寄存器,移位寄存器的电路原理以及verilog代码实现

    寄存器:用以存放二进制代码的电路,下图为由维特阻塞D触发器组成的4位数码寄存器: 逻辑功能分析: 1.异步端CR置0时,输出置0: 2.同步并行置数:D0~D3为4个输入代码,当CP上升沿到达时,D0 ...

  10. OpenFlow和SDN的历史和原理介绍

    OpenFlow相关的历史.新闻:http://blog.csdn.net/jincm13/article/details/7825754起源与发展[https://36kr.com/p/503598 ...