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.
class Solution {
public:
int findComplement(int num) {
int n=,cp,ans;
cp=num;
int m=;
while(cp!=){
cp=cp/;m=m*;
}
ans=m--num;
return ans;
}
};

Bit Manipulation-476. Number Complement的更多相关文章

  1. 【leetcode】476. Number Complement

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

  2. LeetCode#476 Number Complement - in Swift

    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 bits ...

  5. 476. Number Complement

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

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

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

  7. LeetCode 476 Number Complement 解题报告

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

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

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

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

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

  10. LeetCode: 476 Number Complement(easy)

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

随机推荐

  1. redhat配置java环境

    1.检查是否有旧版本的jdk java -version java version "1.4.2" gij (GNU libgcj) version (Red Hat -) Cop ...

  2. 转载 springboot 配置读取

    前言:了解过spring-Boot这个技术的,应该知道Spring-Boot的核心配置文件application.properties,当然也可以通过注解自定义配置文件**.properties的信息 ...

  3. linux 操作笔记

    1.设置防火墙,允许用户使用http访问本机 [root@localhost geoserver]# systemctl enable httpdCreated symlink from /etc/s ...

  4. wpf 进度条 下拉

    <Window x:Class="WpfApplication1.MainWindow"        xmlns="http://schemas.microsof ...

  5. 属性表格 datagridproperty

    http://www.cnblogs.com/yxlblogs/p/3468921.html

  6. HRegionServer异常下线问题

    ==版本== Hadoop:2.7.1 HBase:1.2.1 Zookeeper:3.4.6 ==现象== HBase集群启动一段时间之后,一部分HRegionServer会异常下线重启(用syst ...

  7. mathematica里面清除全部变量

    基本在一个独立代码开始都写: Clear["Global`*"]  (*Clear all variables*) 就可以了

  8. 构建ASP.NET网站十大必备工具

    最近使用ASP.NET为公司构建了一个简单的公共网站(该网站的地址:http://superexpert.com/).在这个过程中,我们使用了数量很多的免费工具,如果把构建ASP.NET网站的必备工具 ...

  9. Binary search tree or not

    https://github.com/Premiumlab/Python-for-Algorithms--Data-Structures--and-Interviews/blob/master/Moc ...

  10. 2018.08.19 洛谷P1402 酒店之王(最大流)

    传送门 最大流入门题,把人拆点即可. 代码: #include<bits/stdc++.h> #define N 505 using namespace std; inline int r ...