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

分析:本题求一个整型数的二进制补数,即对应二进制位取反。

思路:数拆成二进制,同时进行运算,对有效位取反,再组装成十进制数即可。

JAVA CODE

class Solution {
public int findComplement(int num) {
     //将二进制数组(an ... a3 a2 a1 a0)装成十进制。a0+2*(a1+2*(a2+...2*(an+0))) 用递归的思想可以得到很简洁的代码(注意取反操作)。
return (1-num%2)+2*(num==1?0:findComplement(num/2));
}
}
												

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

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

  5. LeetCode#476 Number Complement - in Swift

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

  6. LeetCode 476. Number Complement

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

  7. 476. Number Complement

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

  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. Python验证码通过pytesser识别

    Python安装包: 需要安装的包主要有两个: PIL 和 pytesser .tesseract (1).安装PIL:下载地址:http://www.pythonware.com/products/ ...

  2. spring配置和注解事务同时存在导致的事务嵌套

    详见:http://blog.yemou.net/article/query/info/tytfjhfascvhzxcyt123 首先先看配置文件: [html] view plaincopy < ...

  3. WebServices 之 WSDL

    详见:http://blog.yemou.net/article/query/info/tytfjhfascvhzxcyt234 一,WSDL概述 WebServices Description La ...

  4. Maven(一)初识Maven

    前言 在这之前一直都有去看关于Maven的相关介绍,但是没有到真正要用的时候,自己总是以为懂了.其实真的感觉Maven并没有想象的那么简单! 那我们该怎么去学习maven呢?接下来我将从: 初步认识m ...

  5. poj 3592 缩点+SPFA

    题意:给出一个矩阵,其中#代表墙,不可走,0-9代表权值,*代表可以选择传送.求从0,0点开始出发能获得最大权值. 思路:因为*的出现会有环的情况,先建图连边,将环进行Tarjan缩点,之后再从0,0 ...

  6. Eslint配置

    //ESLint 4.5.0,参考了eslint官方以及alloyteam团队配置 module.exports = { parser: 'babel-eslint', parserOptions: ...

  7. RAID基础知识总结

    1.RAID RAID:Redundant Arrays of Inexpensive(Independent)Disks,即独立磁盘冗余阵列,简称磁盘阵列.简单地说就是把多个独立的硬盘组合起来,从而 ...

  8. 1001.A+B Format (20)的解题

    关于A+B的正确打开方式! 解题思路 gitub 也是研究了很久才学会了本地上传,中间还遇到一些问题,多亏学长的教程跟搜索引擎的帮忙解决啦! 我想还是了解题目的意思是解题的最关键,通过了查词软件跟自身 ...

  9. JAVA基础第二组(5道题)

    6.[程序6] 题目:输入两个正整数m和n,求其最大公约数和最小公倍数.        1.程序分析:利用辗除法. package com.niit.homework1; import java.ut ...

  10. 201521123108 《Java程序设计》第7周学习总结

    1. 本周学习总结 2. 书面作业 Q1.ArrayList代码分析 Q1.1 解释ArrayList的contains源代码 答:源代码如下: public boolean contains(Obj ...