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. 《MVC架构下网站的设计与实现》论文笔记(十八)

    标题:MVC架构下网站的设计与实现 一.基本信息 时间:2017 来源:广东海洋大学数学与计算机学院 关键词:网站设计:MVC 框架:数据库:网络安全 二.研究内容 1.系统的整体架构设计(以广东海洋 ...

  2. 2019牛客暑期多校训练营(第八场)A:All-one Matrices(广告牌问题 单调队列)

    题意:给出N*M的01矩阵,求矩阵个数,满足矩阵内全是‘1’,,而且被至少一个’0‘围住.(假设边界外是‘0’.(N,M<3000) 思路:这类问题,一般解决就是两个方向: A:压缩一维,即枚举 ...

  3. 推荐系统(recommender systems):均值归一化(mean normalization)

    均值归一化可以让算法运行得更好. 现在考虑这样一个情况:一个用户对所有的电影都没有评分,即上图所示 的Eve用户.现在我们要学习特征向量(假设n=2) 以及用户5的向量θ(5),因为用户Eve没有对任 ...

  4. TestNg用例管理

    一.TestNg的数据驱动 1. 数据驱动 1)什么是数据驱动? ·数据驱动是指在一个脚本固定的情况下,用数据来控制该脚本是否运行,以及运行的次数,还有每次运行时对应的参数 2)数据驱动的应用场景: ...

  5. MySQL中经典的too many connection怎么破

    文章来源:云栖社区,经同意授权转载 链接:https://yq.aliyun.com/articles/226984?spm=5176.8091938.0.0.nCksaV 错误解决记录:java d ...

  6. springBoot 利用Idea打包部署

    springBoot 打包部署 1 项目如图: 2 依赖打包插件 3 打包操作 4 运行项目:

  7. can总线学习网上资料汇总

    https://zhuanlan.zhihu.com/p/30247549?utm_source=wechat_session&utm_medium=social&utm_oi=280 ...

  8. 使用haproxy 2.0 prometheus metrics 监控系统状态

    haproxy 2.0 已经发布一段时间了,提供内部直接暴露的prometheus metrics 很方便 ,可以快速的监控系统的状态 以下是一个简单的demo 环境准备 docker-compose ...

  9. python格式化输出之format用法

    format用法 相对基本格式化输出采用‘%’的方法,format()功能更强大,该函数把字符串当成一个模板,通过传入的参数进行格式化,并且使用大括号‘{}’作为特殊字符代替‘%’ 使用方法由两种:b ...

  10. 记一次CPU使用100%问题排查

    需求 前端同事说测试环境的服务接口查起来很慢,很不稳定,不是个别接口,而是大量接口. 情况分析 由于是在测试环境联调,没有多少用户量.第一步:先去服务器看看资源的使用情况.使用top命令,查看cpu的 ...