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. union-find算法Java实现

    package practice; /*在一个全是点的图中,循环选择两点连通,之后判断两点是否在同一通路*/ public class Testmain { public static void ma ...

  2. html5 响应式布局(媒体查询)

    响应式布局        响应式布局,简而言之,就是一个网站能够兼容多个终端--而不是为每个终端做一个特定的版本.这个概念是为解决移动互联网浏览而诞生的.        响应式布局可以为不同终端的用户 ...

  3. plsql developer 恢复默认布局界面

    tools-preferences-appearance-(reset docking,reset toolbars)

  4. 那些年,我们不懂的却又不得不提的 JAVA异常和异常处理!

    ---恢复内容开始--- 首先,我是个小小的菜鸟,最近突然突发奇想,想研究一下java的异常和异常的处理,稍有些理解,老鸟们莫要嘲笑... 既然要讲异常和异常的处理,我们就要先了解异常,那么,什么是异 ...

  5. Python 异常处理

    Python 异常处理 python提供了两个非常重要的功能来处理python程序在运行中出现的异常和错误.你可以使用该功能来调试python程序. 异常处理: 本站Python教程会具体介绍. 断言 ...

  6. WEB跨域资源共享:Cross-origin Resource Sharing(CORS)

    跨域资源共享(CORS):浏览器同源策略中的同源指协议+域名+端口三者完全一致,其中任何一个不同即为跨域 1. 浏览器同源政策是隔离潜在恶意文件的安全机制,限制信息传递和使用的边界,不是信息的保密机制 ...

  7. 【Alpha】第三次Daily Scrum Meeting

    GIT 一.今日站立式会议照片 二.会议内容 1.确定开发人员负责开发模块 开发人员 开发模块 杨嘉成 注册登陆模块 吴文庆 服务模块 程志铭 个人中心 2.测试人员在开发人员完成该模块后紧跟测试 三 ...

  8. spring+mybatis的简单配置示例

    简单代码结构: //Book.java package com.hts.entity; public class Book { private String id; private String bo ...

  9. 201521123105 第10周Java学习总结

    1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结异常与多线程相关内容. 2. 书面作业 本次PTA作业题集异常.多线程 1.finally 题目4-21.1 截图你的提交结果(出现 ...

  10. 让你的python程序同时兼容python2和python3

    python邮件列表里有人发表言论说「python3在10内都无法普及」.在我看来这样的观点有些过于悲观,python3和python2虽然不兼容,但他们之间差别并没很多人想像的那么大.你只需要对自己 ...