[LeetCode] 476. Number Complement_Easy tag: Bit Manipulation
这个题目思路就是比如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的更多相关文章
- LeetCode#476 Number Complement - in Swift
Given a positive integer, output its complement number. The complement strategy is to flip the bits ...
- LeetCode 476. Number Complement (数的补数)
Given a positive integer, output its complement number. The complement strategy is to flip the bits ...
- LeetCode 476. Number Complement
Given a positive integer, output its complement number. The complement strategy is to flip the bits ...
- LeetCode 476 Number Complement 解题报告
题目要求 Given a positive integer, output its complement number. The complement strategy is to flip the ...
- [LeetCode] 190. Reverse Bits_Easy tag: Bit Manipulation
Reverse bits of a given 32 bits unsigned integer. Example: Input: 43261596 Output: 964176192 Explana ...
- LeetCode: 476 Number Complement(easy)
题目: Given a positive integer, output its complement number. The complement strategy is to flip the b ...
- 【leetcode】476. Number Complement
problem 476. Number Complement solution1: class Solution { public: int findComplement(int num) { //正 ...
- [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 ...
- [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 ...
随机推荐
- Web编辑器的使用
1.复制web编辑器到你的项目中的表现层(UI) 2.添加引用:FredCK.FCKeditorV2.dll到你的项目中来 3.页面中加引用 <%@ Register TagPrefix=&qu ...
- 【Java基础系列】Java IO系统
前言 创建好的输入/输出系统不仅要考虑三种不同种类的IO系统(文件,控制台,网络连接)还需要通过大量不同的方式与他们通信(顺序,随机访问,二进制,字符,按行,按字等等). 一.输入和输出 Java的I ...
- git-常用命令一览表
一.git bash 操作文件 常用命令表 git常用 文件操作 命令 功能分类 命令 说明 备注 目录切换 cd 文件目录 改变/切换 目录, change directory 如 cd e:\ff ...
- Linux 常用查找文件或者文件内容
举例树形图 .|-- test_dir| `-- dir_test_doc.text|-- test_dir2| |-- dir2_test_doc.txt| `-- dir2_test_doc2.t ...
- 【NOI2015】荷马史诗[Huffman树+贪心]
#130. [NOI2015]荷马史诗 统计 描述 提交 自定义测试 追逐影子的人,自己就是影子. ——荷马 Allison 最近迷上了文学.她喜欢在一个慵懒的午后,细细地品上一杯卡布奇诺,静静地阅读 ...
- IOS 6 和 IOS7 UITableViewCell上添加控件的获取
假设每个cell上面都有UIButton,怎么判断哪个Cell上的按钮被按下了呢? IOS6上 -(IBAction)btnClick:(id)sender { UIButton *btn = (UI ...
- 【CF887E】Little Brother 二分+几何
[CF887E]Little Brother 题意:给你n个圆和一条线段,保证圆和圆.圆和线段所在直线不相交,不相切,不包含.求一个过线段两端点的圆,满足不和任何圆相交(可以相切.包含).问圆的最小半 ...
- python pytest测试框架介绍二
在介绍一中简单介绍了pytest的安装和简单使用,接下来我们就要实际了解pytest了 一.pytest的用例发现规则 pytest可以在不同的函数.包中发现用例,发现的规则如下 文件名以test_开 ...
- mysql 数据库简介
1. 什么是数据库 数据库(Database)是按照数据结构来组织.存储和管理数据的仓库, 每个数据库都有一个或多个不同的API用于创建,访问,管理,搜索和复制所保存的数据. 我们也可以将数据存储在文 ...
- 【技术开放日】msup携手HPE揭秘全球测试中心背后的12条技术实践
保证软件产品质量是软件测试永恒的目标. 以控制为出发点的传统IT时代正在快速的向以激活生产力为目的的移动互联时代转变.这不仅是技术的升级,更是思想意识的巨大变革,也对软件技术的发展带来的更高的要求和更 ...