题目:

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. java 最长回文字串

      package string.string1_6; public class LongestPalidrome { /** * 使用常规方法, 以字符串的每一个字符作为中心进行判断, 包括奇数和偶 ...

  2. linux快捷键及主要命令(转载)

    作者:幻影快递Linux小组 翻译 2004-10-05 22:03:01 来自:Linux新手管理员指南(中文版) 5.1 Linux基本的键盘输入快捷键和一些常用命令 5.2 帮助命令 5.3 系 ...

  3. git和github菜鸟使用步骤

    刚刚在windows7下安装完git.奉上安装步骤. git安装 安装git程序.运行以下操作: 1. $ cd ~/.ssh    //检查计算机ssh密钥 2.假设没有提示:No such fil ...

  4. UNIX网络编程卷1 时间获取程序client TCP 使用非堵塞connect

    本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie 1.当在一个非堵塞的 TCP 套接字(可使用 fcntl 把套接字变成非堵塞的)上调用 co ...

  5. 小白学开发(iOS)OC_ 字符串重组(2015-08-13)

    // //  main.m //  字符串重组 // //  Created by admin on 15/8/13. //  Copyright (c) 2015年 admin. All right ...

  6. iOS8的UIPresentationController

    本文转载至 http://kyfxbl.iteye.com/blog/2147888 从iOS8开始,controller之间的跳转特效,需要用新的API UIPresentationControll ...

  7. js怎么限制文本框input只能输入数字

    1.说明 本篇文章介绍怎么使用js限制文本框只能输入数字 2.HTML代码 <!DOCTYPE html> <html xmlns="http://www.w3.org/1 ...

  8. 工作室成员 GitHub 地址集中贴(按发布时间先后排序)

    金质行 https://github.com/jinxiaohang/ 金林超 https://github.com/jinlinchao/ 王贤国 https://github.com/ErhuoH ...

  9. svn下载

    首先介绍的是SVN安装包的下载,分别包括服务器版和客户端版 下载地址:http://subversion.apache.org/packages.html 打开后,点击Windows 分别下载客户端( ...

  10. 7.JavaScript变量

    VAR声明变量,一条语句可以声明多个变量 var name="Gates", age=56, job="CEO"; Value = undefined 在计算机 ...