这个题目思路就是比如101 的结果是010, 可以从111^101 来得到, 那么我们就需要知道刚好比101多一位的1000, 所以利用 while i <= num : i <<= 1, 去得到1000, 然后-1, 便得到111,

再跟num ^, 也就是异或即可.

Code

class Solution(object):
def findComplement(self, num):
i = 1
while i <= num:
i <<= 1
return (i-1)^num

[LeetCode] 476. Number Complement_Easy tag: Bit Manipulation的更多相关文章

  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] 190. Reverse Bits_Easy tag: Bit Manipulation

    Reverse bits of a given 32 bits unsigned integer. Example: Input: 43261596 Output: 964176192 Explana ...

  6. LeetCode: 476 Number Complement(easy)

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

  7. 【leetcode】476. Number Complement

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

  8. [LeetCode] 711. Number of Distinct Islands II_hard tag: DFS

    Given a non-empty 2D array grid of 0's and 1's, an island is a group of 1's (representing land) conn ...

  9. [LeetCode] 694. Number of Distinct Islands

    Given a non-empty 2D array grid of 0's and 1's, an island is a group of 1's (representing land) conn ...

随机推荐

  1. Root用户安装MariaDB到 /usr/local/mysql

    参考地址: http://www.zhdba.com/mysqlops/2013/08/16/mariadb-cn_1001/ https://mariadb.com/kb/en/mariadb/in ...

  2. GlusterFS六大卷模式說明

    GlusterFS六大卷說明   第一,分佈卷 在分布式卷文件被随机地分布在整个砖的体积.使用分布式卷,你需要扩展存储,冗余是重要或提供其他硬件/软件层.(簡介:分布式卷,文件通过hash算法随机的分 ...

  3. ldap 配置过程详解

    ldap常用名称解释 1.环境搭建 操作系统:centos6.5 x86_64关闭防火墙.selinux开启时间同步# crontab -e加入# time sync*/5 * * * * /usr/ ...

  4. 使用DnsCat反弹shell

    DnsCat技术特点 Dns隧道反弹shell DnsCat服务器的安装 #git clone https://github.com/iagox86/dnscat2.git #cd dnscat2 # ...

  5. MySQL知识小结

    MySQL的知识面试中还是经常被问到的,简单的使用似乎无法达到面试官的要求,很多问题会关于Mysql存储引擎,所以这里还是需要系统学习一下Mysql的一些知识,面试过程中游刃有余. MySQL体系结构 ...

  6. 算法题目-记hulu失败的实习面试

    1.对于数组A[0,1,2,3,4,...,k],求得0<=i < j < k,且使得A[j] - A[i]为最大值. 最简单也最容易想到的搜索两遍,即可得到答案.i的位置从起始至倒 ...

  7. 使用zsh 替换 bash

    摘自:http://macshuo.com/?p=676#wechat_redirect Shell是Linux/Unix的一个外壳,你理解成衣服也行.它负责外界与Linux内核的交互,接收用户或其他 ...

  8. Unity3D笔记 切水果二 刀光剑影

    一.步骤一创建一个空GameObject.js 二.代码 #pragma strict var myColor:Color; var firstPosition:Vector3;//鼠标点击的第一个点 ...

  9. yii的安装

    1.安装composer windows系统直接下载Composer-Setup.exe 运行安装. 2.安装Composer asset plugin composer安装完成后,在一个可通过web ...

  10. vue--子组件主动获取父组件的数据和方法

    子组件主动获取父组件的数据和方法 简单示例: this.$parent.数组 this.$parent.方法 示例: <template> <div id="Header& ...