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. springboot项目突然启动缓慢

    在项目快到最后的时候,有一次在本地启动项目的时候,突然发现项目启动起来特别的慢. 刚开始也不知道哪里出了问题,只能慢慢的查原因. springboot项目在debug模式下本来运行的挺快,后来某一天突 ...

  2. python应用-传入年月日 输出为一年的第几天

    ef leap_year(year): return (year//4==0 and year//100!=0) or (year //400==0) def which_day(year,month ...

  3. python应用-输入分数 输出最高分数对应的名字

    def main(): names = ['刘备', '张飞', '曹操', '袁绍', '关羽', '赵云', '周瑜'] scores=[] num=0 m=0 for name in names ...

  4. 安装GO

    1.中文社区 下载地址   https://studygolang.com/dl    选择自己操作系统版本 2.找到适合你系统的版本下载,本人下载的是windows版本.也可以下载Source自己更 ...

  5. [iOS] 利用 NSAttributedString 进行富文本处理

    /iOS /[iOS] 利用 NSAttributedString 进行富文本处理 2016年4月4日 刘小龙 iOS 许多时候我们需要以各种灵活的形式展现文本信息,即富文本.普通的 text 属性显 ...

  6. 在WinDbg中显示和搜索std::vector内容

    WinDbg从来都不擅长可视化.尽管Visual Studio一直都有autoexp.dat,而且最近还出现了本机调试器可视化工具,但WinDbg用户不得不满足于转储内存区域和搜索内存来识别模式.另一 ...

  7. RFM - Customer Level Data

    Introduction ## Warning: package 'DT' was built under R version 3.5.2 RFM (recency, frequency, monet ...

  8. 钠 GZY整理贪心

    目录 CF140C New Year Snowmen CF161B Discounts P1842 奶牛玩杂技 CF140C New Year Snowmen #include <bits/st ...

  9. python实现余弦相似度文本比较

    向量空间模型VSM: VSM的介绍: 一个文档可以由文档中的一系列关键词组成,而VSM则是用这些关键词的向量组成一篇文档,其中的每个分量代表词项在文档中的相对重要性. VSM的例子: 比如说,一个文档 ...

  10. Luogu P2447 [SDOI2010]外星千足虫 高斯消元

    链接 给出的条件是异或类型的方程,可以直接用bitset优化高斯消元. 至于求K,在高斯消元时记录用到的最大的方程的编号即可. 代码: // luogu-judger-enable-o2 #inclu ...