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 ...
随机推荐
- 【emWin】例程二十八:窗口对象——Menu
简介: MENU 小工具可用于创建若干种菜单.每个菜单项代表一个应用程序命令或 子菜单.MENU 可水平显示和/ 或垂直显示.菜单项可使用分隔符进行分组.水 平菜单和垂直菜单均支持分隔符.选择一个菜单 ...
- 命名实体识别,使用pyltp提取文本中的地址
首先安装pyltp pytlp项目首页 单例类(第一次调用时加载模型) class Singleton(object): def __new__(cls, *args, **kwargs): if n ...
- linux下yum安装最新稳定版nginx
## 摘抄nginx官网文档 URL:http://nginx.org/en/linux_packages.html#stable To set up the yum repository for R ...
- Java知多少(84)图形界面之布局设计
在界面设计中,一个容器要放置许多组件,为了美观,为组件安排在容器中的位置,这就是布局设计.java.awt中定义了多种布局类,每种布局类对应一种布局的策略.常用的有以下布局类: FlowLayout, ...
- Angular4学习笔记(三)- 路由
路由简介 路由是 Angular 应用程序的核心,它加载与所请求路由相关联的组件,以及获取特定路由的相关数据.这允许我们通过控制不同的路由,获取不同的数据,从而渲染不同的页面. 相关的类 Routes ...
- python -u 启动python文件的作用,PYTHONUNBUFFERED环境变量的作用
python -u 启动python文件的作用是不缓存,直接把输出重定向到文件,比如nohup启动什么的,如果不使用-u启动,那么程序中的print和日志什么的,可能不会非常及时的重定向到out文件, ...
- [Bayes] Improve HMM step by step
以下是HMM,当emission probability变为高斯时,只需改变其中相关部分即可,也就是下图最后一行. 如下可见,在优化过程中套路没有太大的影响,但变为高斯后表达变得更精确了呢. 当然,这 ...
- [Unity3D] 05 - Access to DB or AWS
可以选择连接本地服务器,或者云服务器. 参考源代码 : https://www.cnblogs.com/wuzhang/p/wuzhang20141202.html (1) 功能:点击一下按键,然后访 ...
- [PHP] 09 - PHP 7 & Tricky
新特征列表: 序号 内容 1 PHP 标量类型与返回值类型声明 2 PHP NULL 合并运算符 3 PHP 太空船运算符(组合比较符) 4 PHP 常量数组 5 PHP 匿名类 6 PHP Clos ...
- rar压缩find查找到的文件
find . -name 'CMakeLists.txt' | xargs /d/Program\ Files/WinRAR/rar.exe a -r ./out.rar $ ------------ ...