题目要求

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.

题目分析及思路

题目给出一个正整数,要求得到它的complement number。complement number是先对原正整数取二进制,然后各位取反,最后再化为十进制的数。可以先获得原正整数的各位数,然后与1依次异或,将结果乘以权并求和。

python代码

class Solution:

def findComplement(self, num: 'int') -> 'int':

b = []

res = 0

while num:

b.append(num % 2)

num //= 2

for i in range(len(b)):

b[i] ^= 1

res += (b[i] * math.pow(2, i))

return int(res)

LeetCode 476 Number Complement 解题报告的更多相关文章

  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(easy)

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

  5. 【LeetCode】137. Single Number II 解题报告(Python)

    [LeetCode]137. Single Number II 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/problems/single- ...

  6. 【leetcode】476. Number Complement

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

  7. LeetCode 1 Two Sum 解题报告

    LeetCode 1 Two Sum 解题报告 偶然间听见leetcode这个平台,这里面题量也不是很多200多题,打算平时有空在研究生期间就刷完,跟跟多的练习算法的人进行交流思想,一定的ACM算法积 ...

  8. 【LeetCode】01 Matrix 解题报告

    [LeetCode]01 Matrix 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/01-matrix/#/descripti ...

  9. 【LeetCode】120. Triangle 解题报告(Python)

    [LeetCode]120. Triangle 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址htt ...

随机推荐

  1. Basic SAP Data Types

    Basic SAP Data Types 6 out of 11 rated this helpful - Rate this topic The parameter types that the M ...

  2. 【Unity】EasyTouch5触屏检测

    Unity AssetStore地址    https://assetstore.unity.com/packages/tools/input-management/easy-touch-5-touc ...

  3. Cordova开发App入门之创建android项目

    Apache Cordova是一个开源的移动开发框架.允许使用标准的web技术-HTML5,CSS3和JavaScript做跨平台开发. 应用在每个平台的具体执行被封装了起来,并依靠符合标准的API绑 ...

  4. java中的数据加密4 数字签名

    数字签名 它是确定交换消息的通信方身份的第一个级别.A通过使用公钥加密数据后发给B,B利用私钥解密就得到了需要的数据,问题来了,由于都是使用公钥加密,那么如何检验是A发过来的消息呢?上面也提到了一点, ...

  5. java.lang.ClassCastException: net.sf.ezmorph.bean.MorphDynaBean cannot be cast to

    Java.lang.ClassCastException: net.sf.ezmorph.bean.MorphDynaBean cannot be cast to 在使用JSONObject.toBe ...

  6. [UI] 02 - Layout & CSS3

    开胃小菜 一.纯CSS布局 UI效果 From: 创建一个没有表格的网页 代码详情 <!DOCTYPE html> <html> <head> <meta c ...

  7. 09观察者模式ObServer

    一.什么是观察者模式 Observer模式是行为模式之一,它的作用是当 一个对象的状态发生变化时,能够自动通知其他 关联对象,自动刷新对象状态. Observer模式提供给关联对象一种同步通信的 手段 ...

  8. ios开发之--valueForKeyPath的用法

    1.获取数组中的平均值,最大值,最小值,总和,代码如下: NSArray *ary = @[@,@,@,@,@,@,@]; [self caculateArray:ary]; -(NSString * ...

  9. [Cubieboard] 安装 Lubuntu server for SDCard

    硬件:Cubieboard2 系统:Ubuntu 14.04 based on VMware Virtual Machine 镜像:cb2-lubuntu-server-tsd-tfcard-v2.0 ...

  10. Clover 3 --- Windows Explorer 资源管理器的一个扩展,为其增加类似谷歌 Chrome 浏览器的多标签页功能。

    http://cn.ejie.me/ http://cn.ejie.me/uploads/setup_clover@3.4.6.exe  软件下载 默认图标实在比较难看,更换图标 更改图标---选择图 ...