LeetCode——Number Complement

Question

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

Note:

The given integer is guaranteed to fit within the range of a 32-bit signed integer.

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.

具体实现

class Solution {
public:
int findComplement(int num) {
int i = 0;
while (num >= pow(2, i)) {
i++;
}
int value = pow(2, i > 0 ? i : 1) - 1;
return value ^ num; }
};

LeetCode——Number Complement的更多相关文章

  1. [LeetCode] Number Complement 补数

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

  2. Leetcode: Number Complement

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

  3. LeetCode#476 Number Complement - in Swift

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

  4. 【leetcode】476. Number Complement

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

  5. LeetCode_476. Number Complement

    476. Number Complement Easy Given a positive integer, output its complement number. The complement s ...

  6. 2016.5.15——leetcode:Number of 1 Bits ,

    leetcode:Number of 1 Bits 代码均测试通过! 1.Number of 1 Bits 本题收获: 1.Hamming weight:即二进制中1的个数 2.n &= (n ...

  7. LeetCode——Number of Boomerangs

    LeetCode--Number of Boomerangs Question Given n points in the plane that are all pairwise distinct, ...

  8. LeetCode_Easy_471:Number Complement

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

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

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

随机推荐

  1. 51NOD 1013(3的幂的和)

    题目链接:传送门 题目大意:求(3^0+3^1+3^2+3^3+...+3^n)%1e9的值 题目思路:乘法逆元裸题 #include <iostream> #include <cs ...

  2. Entity Framework查询生成大量的子查询,如何避免?求救

    最近使用Entity Framework做一个中型的项目,一张表含有千万条数据,并没有使用很复杂的查询,只是程序上使用了DTO进行帅选数据,且使用了分页,效果很不理想.经过跟踪sql,我发现很多简单的 ...

  3. wire_format.cc:1091] String field 'accountid' contains invalid UTF-8 data when serializing a protocol buffer. Use the 'bytes' type if you intend to send raw bytes.

    原因: 在protobuf 的string字段中存在中文,序列化的时候会出现截断数据,string这个类型带有检查功能 解决方法: 把protobuf中存在中文的string字段类型 改为bytes ...

  4. ini_set('date.timezone','Asia/Shanghai');

    w 同样的代码,不一样的php ENV.

  5. nodejs使用——以elasticsearch-exporter为例

    安装nodejs: yum install nodejs 运行node命令查看是否安装成功: 可以看到成功进入命令行,安装成功. node命令前面要加点,使用 .help 查看有哪些命令: 使用.ex ...

  6. 我的Android进阶之旅------>关于使用Android Studio替换App的launcher图标之后仍然显示默认的ic_launcher图标的解决方法

    前言 最近做了一个App,之前开发该App的时候一直以来都是默认的launcher图标启动的, 今天美工换了一个App的launcher 图标,因此在Android Studio中将默认的lanche ...

  7. node.js---sails项目开发(2)

    1.安装mongoDB,这里用brew安装 brew install mongodb 2. 启动数据库 mongod 3.再打开一个终端,连接数据库 mongo 4.启动成功后,接下来就是新建一个数据 ...

  8. SAP系统接口方式:

    SAP系统接口方式: 1.PI - 信使中间件 (大公司多选择) 数据: SAP- PI- U8 U8- PI- SAPPI 底层用的还是webservice 技术优点:实时性高: 可处理大数据(在调 ...

  9. xcode中全文查询某个中文字

    查询所有中文 [^"]*[\u4E00-\u9FA5]+[^"\n]*? 查询某个中文字“中”字 [^"]*[\u4e2d]+[^"\n]*? 中文字转成uni ...

  10. ngs中reads mapping-pku的生信课程

    4.NGS中的reads mapping 顾名思义,就是将测序的得到的DNA定位在基因组上. 因为二代测序的得到的序列是较短的,reads mapping很好地解决了这个问题. 本质上reads ma ...