LeetCode 476 Number Complement 解题报告
题目要求
Given a positive integer, output its complement number. The complement strategy is to flip the bits of its binary representation.
Note:
- The given integer is guaranteed to fit within the range of a 32-bit signed integer.
- 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 解题报告的更多相关文章
- 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(easy)
题目: Given a positive integer, output its complement number. The complement strategy is to flip the b ...
- 【LeetCode】137. Single Number II 解题报告(Python)
[LeetCode]137. Single Number II 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/problems/single- ...
- 【leetcode】476. Number Complement
problem 476. Number Complement solution1: class Solution { public: int findComplement(int num) { //正 ...
- LeetCode 1 Two Sum 解题报告
LeetCode 1 Two Sum 解题报告 偶然间听见leetcode这个平台,这里面题量也不是很多200多题,打算平时有空在研究生期间就刷完,跟跟多的练习算法的人进行交流思想,一定的ACM算法积 ...
- 【LeetCode】01 Matrix 解题报告
[LeetCode]01 Matrix 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/01-matrix/#/descripti ...
- 【LeetCode】120. Triangle 解题报告(Python)
[LeetCode]120. Triangle 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址htt ...
随机推荐
- HttpPost请求将json作为请求体传入的简单处理方法
https://www.cnblogs.com/mambahyw/p/7875142.html **************************************************** ...
- linux(mac) 编译安装MySQL
Reference: https://blog.csdn.net/Tzhennan/article/details/80565235 官方下载地址: https://dev.mysql.com/do ...
- <我的股票交易知识汇总与个人感悟_v1.0 (By geman)>
书在这里 一个完整的股票交易包括选股.买股.持股.卖股四个阶段. 右侧交易,顶是跌出来的,底是涨出来的 一定要敢于止损,设好止损位,严格执行,即使踏空也无怨无悔:资金安全第一位 坚持只买处于上升通道的 ...
- Cocos2d-x 3.0 纹理
1.纹理控制. Sprite *pSprite = Sprite::create("background.png"); TexParams params = {GL_NEAREST ...
- iOS Xcode, 解决“Could not insert new outlet connection”的问题。
在Xcode中,我们能够在StoryBoard编辑界面或者是xib编辑界面中通过"Control键+拖拽"的方式将某个界面元素和相应的代码文件连接起来,在代码文件里创建outlet ...
- SQLServer最耗资源时间的SQL语句
作者kolachen http://blog.sina.com.cn/s/blog_63f3e0060102vcm0.html 先拷过来了,等有空再研究一下 执行最慢的SQL语句 SELECT (to ...
- linux sysfs文件系统
个人理解:sysfs向用户空间展示了驱动设备的层次结构.我们都知道设备和对应的驱动都是由内核管理的,这些对于用户空间是不可见的.现在通过sysfs,可以在用户空间直观的了解设备驱动的层次结构. 我们来 ...
- SQL Server 2012 R2升级到SQL Server 2016无法正常启动数据库服务解决方案
原定周末把公司的TFS升级到2018,由于TFS 2018需要SQL Server至少是2016以上版本,所以还需要将原来的SQL Server 2012 R2一并升级.今天早上负责的同事告诉我升级失 ...
- [转]获取app的内部储存路径
首先内部存储路径为/data/data/youPackageName/,下面讲解的各路径都是基于你自己的应用的内部存储路径下.所有内部存储中保存的文件在用户卸载应用的时候会被删除. 一. files1 ...
- bochs
● 制作一个硬盘 ./bximage 步骤与制作软盘的相似,完成后将bochs软件提示的最后一句话,添加到自己的配置文件里: dd if=loader.bin of=~/Softwares/bochs ...