题目:

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:
Output:
Explanation: The binary representation of is (no leading zero bits), and its complement is . So you need to output .

代码:

 class Solution {
public:
int findComplement(int num) {
int x = , p = ;
while(num){
if(num%==) x |= ( << p);
++p;
num/=;
}
return x;
}
};

LeetCode: 476 Number Complement(easy)的更多相关文章

  1. LeetCode#476 Number Complement - in Swift

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

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

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

  3. LeetCode 476. Number Complement

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

  4. LeetCode 476 Number Complement 解题报告

    题目要求 Given a positive integer, output its complement number. The complement strategy is to flip the ...

  5. 【leetcode】476. Number Complement

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

  6. 【LeetCode】476. Number Complement (java实现)

    原题链接 https://leetcode.com/problems/number-complement/ 原题 Given a positive integer, output its comple ...

  7. [LeetCode&Python] Problem 476. Number Complement

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

  8. 476. Number Complement

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

  9. 476. Number Complement 二进制中的相反对应数

    [抄题]: Given a positive integer, output its complement number. The complement strategy is to flip the ...

随机推荐

  1. POJ 3469(Dual Core CPU-最小割)[Template:网络流dinic V2]

    Language: Default Dual Core CPU Time Limit: 15000MS   Memory Limit: 131072K Total Submissions: 19321 ...

  2. vue 路由组件不重新加载

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  3. 基于LRU Cache的简单缓存

    package com.test.testCache; import java.util.Map; import org.json.JSONArray; import org.json.JSONExc ...

  4. 从架构层面杜绝lua中使用未定义的变量

    # 从架构层面杜绝lua中使用未定义的变量 标签(空格分隔): lua --- lua中有一个很坑的地方:1.就是如果一个变量拼写错误,会自动的认为你定义了一个值为nil的全局变量.2.如果在func ...

  5. java使用ftp局域网内多线程上传图片过慢

    多线程ftp上传文件时候,图片上传很慢,调试和查询资料发现主要在:storeFile方法 解决方案如下: FTPClient fc设置setBufferSize 可以根据内存大小适当设置大点的缓冲区: ...

  6. 5.JavaScript改变样式,验证用户输入

    ① x=document.getElementById("demo") //找到元素 x.style.color="#ff0000"; //改变样式 ② if ...

  7. 用css3技术给网站加分

    自己写了几个小DEMO,请打开http://codepen.io/shenggen/

  8. spring事件广播

    可参考:http://www.cnblogs.com/atyou/archive/2013/01/07/2850106.html 其中的类图更是精彩,现截至如下:

  9. Codeforces Round #379 (Div. 2) D. Anton and Chess —— 基础题

    题目链接:http://codeforces.com/contest/734/problem/D D. Anton and Chess time limit per test 4 seconds me ...

  10. NSMutableURLRequest,在POST方式下传递参数

    1. [代码][C/C++]代码         NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];     NSUs ...