当前位置:网站首页>476. Number Complement
476. Number Complement
2022-06-22 13:15:00 【Sterben_ Da】
476. Number Complement
Easy
2035106Add to ListShare
The complement of an integer is the integer you get when you flip all the 0's to 1's and all the 1's to 0's in its binary representation.
- For example, The integer
5is"101"in binary and its complement is"010"which is the integer2.
Given an integer num, return its complement.
Example 1:
Input: num = 5 Output: 2 Explanation: The binary representation of 5 is 101 (no leading zero bits), and its complement is 010. So you need to output 2.
Example 2:
Input: num = 1 Output: 0 Explanation: The binary representation of 1 is 1 (no leading zero bits), and its complement is 0. So you need to output 0.
Constraints:
1 <= num < 231
Note: This question is the same as 1009: Loading...
class Solution:
def findComplement(self, num: int) -> int:
"""
assert Solution().findComplement(5) == 2
assert Solution().findComplement(1) == 0
assert Solution().findComplement(8) == 7
assert Solution().findComplement(13) == 2
Their thinking : take num Shift to the right continuously to get the last digit , If last digit 0, take 1 Shift the number of cycles bit to the left and add it to the result number
Time complexity :O(logn)
"""
sign, result = 0, 0
while num != 0:
if num & 1 == 0:
result += 1 << sign
num >>= 1
sign += 1
return result边栏推荐
- SiCf batch activation service node
- If Tiankeng majors learn IC design by themselves, will any company want it
- SNC processing failed SAP Router证书重新生成
- 257. Binary Tree Paths
- Isn't this another go bug?
- leetcode 32. 最长有效括号
- Secondary development of robotframework -- file parsing
- 241. Different Ways to Add Parentheses
- Sequoiadb distributed database may 2022 issue
- 257. Binary Tree Paths
猜你喜欢

The Chinese display of SAP client is garbled

Windows下MySQL 8.0.29的详细安装教程,解决找不到VCRUNTIME140_1.dll、plugin caching_sha2_password could not be loaded

46. Permutations

6月《中国数据库行业分析报告》发布!智能风起,列存更生

257. Binary Tree Paths

In C # development, the third-party components lambdaparser, dynamicexpresso and z.expressions are used to dynamically parse / evaluate string expressions

Reconstruction practice of complex C-end project of acquisition technology

SAP fi financial statement version setting

SNC processing failed SAP Router证书重新生成

文件下载漏洞&文件读取漏洞&文件删除漏洞
随机推荐
Making rectangular border according to metric system through PostGIS
Isn't the execution process of ODPs SQL executed from top to bottom
剑指 Offer II 114. 外星文字典
MySQL_ Create and manage tables
Reconstruction practice of complex C-end project of acquisition technology
8 challenges of BSS application cloud native deployment
leetcode 第 297 场周赛
Wisdom age voice +php
Arcpy 添加图层到地图文档
PHP deserialization & Magic method
MySQL 5.7 + Navicat 下载安装教程(附安装包)
重磅直播|BizDevOps:数字化转型浪潮下的技术破局之路
Maui uses Masa blazor component library
Is the dynamic table of Flink created in this way? I use the flick CDC to read MySQL data, write the flick dynamic table, and send
OO2022第四单元作业总结
SAP system license viewing application and import
46. Permutations
SAP fi financial statement version setting
Writing a contract testing tool from scratch -- database design
2017 annual summary