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-命令行执行脚本

    原文地址:https://blog.csdn.net/qq_35451939/article/details/79643560 日常测试过程中发现,在大数量并发时,jmeterGUI界面时长宕机.卡死 ...

  2. .net 异常

    ArgumentNullException 当将空引用(在 Visual Basic 中为 Nothing)传递给不接受它作为有效参数的方法时引发的异常.

  3. Intellij IDEA运行报Command line is too long解法

    修改项目下 .idea\workspace.xml,找到标签 <component name="PropertiesComponent"> , 在标签里加一行  < ...

  4. 函数式编程—函数的关系—is-a、has-a、use-a

    is-a:函数的实现与函数类型的关系: has-a:匿名(闭包)函数的创建者与匿名函数的关系:匿名函数与环境和上下文(函数)的关系: use-a:高阶函数与参量函数的关系: 函数式编程的基本功之一就是 ...

  5. LeetCode 911. Online Election

    原题链接在这里:https://leetcode.com/problems/online-election/ 题目: In an election, the i-th vote was cast fo ...

  6. springboot与ssm的差异性

    springboot简化了ssm的配置 将外部jar包改为内部pom.xml文件配置 同时 使用了多种注解来进行注解式的开发 [图1:springboot的一些依赖模块] 通过原springmvc机制 ...

  7. 洛谷P4735题解

    若想要深入学习可持久化0-1Trie树,传送门. Description: 给定数列 \(\{a_n\}\) ,支持两种操作: 在数列尾添加一个数 \(x\) ,数列长度变成 \(n+1\) ; 给定 ...

  8. Shell脚本中$0、$?、$!、$$、$*、$#、$@的意义

    $0 当前脚本的文件名$n 传递给脚本或者函数的参数,脚本后的第n个字符串,n=1…9$# 传递给脚本或者函数的参数个数$? 上一个命名的退出状态,或者函数的返回值(非0表示错误)$$ shell本身 ...

  9. OpenFOAM——圆柱绕流对流换热

    本算例来自<ANSYS FLUENT技术基础与工程应用:流动传热与环境污染控制领域> TOP和DOWN为对称边界(symmetry),入口速度为0.01m/s,入口温度为300K,圆柱温度 ...

  10. adb 常用命令一

    1.install 和uninstall adb -s 设备号 install 安装包路径   adb uninstall package名 2.pull 和push: adb pull /sdcar ...