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. django最小程序开发流程

    1.建立工程 在工程目录下打开cmd,输入以下命令.其中mysite是项目名称. django-admin startproject mysite 命令运行完后,在该目录下会出现一个名为mysite的 ...

  2. Centos7-bond模式介绍

    bond模式: Mode=0(balance-rr)表示负载分担round-robin Mode=1(active-backup)表示主备模式,只有一块网卡是active,另外一块是备的standby ...

  3. 珠峰培训node正式课--【笔记】|全局对象 | process | util | fs | stream 流

    全局对象: console  : __filename     ; __dirname     ; setTimeOut     ; setImmediate(把参数函数放在下一个环节执行) proc ...

  4. post Cache

    http://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html#sec9.5 POST The POST method is used to request ...

  5. Hibernate中对象的三种状态即save(),update(),saveOrUpdate()的使用【转】

    当new一个user的时候,是处于瞬时状态 当调用session.save(user)的时候,user对象会放到session里,此时处于持久状态 当提交事务的时候,user对象才会存到DB中 当调用 ...

  6. Linux下Python3源码安装

    1.下载 wget https://www.python.org/ftp/python/3.7.4/Python-3.7.4.tgz 2.解压 tar -xzvf Python-3.7.4.tgz 3 ...

  7. Filters in ASP.NET Core

    Filters in ASP.NET Core allow code to be run before or after specific stages in the request processi ...

  8. Program terminated with signal 6, Aborted. (最后发现是数组越界导致)

    外网崩溃现象: 1.多台物理机中的多个进程消失,而且都是场景进程. 2.core文件都很小,看了 ulimit -a 和 cat proc/pid/limits  (都很正常.看这个是为了以后core ...

  9. vue 百度地图多标注展示和点击标注进行的提示

    index.html中加入script <script type="text/javascript" src="http://api.map.baidu.com/a ...

  10. docker时区问题

    解决: dockerfile: RUN cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime#update application timezoneR ...