当前位置:网站首页>476. Number Complement
476. Number Complement
2022-06-22 12:26: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
解题思路:将num不断向右移位取末位数,若末位数位0,将1向左移循环次数位添加到结果数里
时间复杂度:O(logn)
"""
sign, result = 0, 0
while num != 0:
if num & 1 == 0:
result += 1 << sign
num >>= 1
sign += 1
return result边栏推荐
- universaldependencies依存关系标签解释
- leetcode 32. 最长有效括号
- 智龄语音+php
- Tianyi cloud explores a new idea of cloud native and edge computing integration
- 0179 largest number
- The Chinese display of SAP client is garbled
- 巨杉数据库荣获艾媒咨询2022年中国信创产业双项荣誉
- redis修改密码,及启动、查看等操作
- 693. Binary Number with Alternating Bits
- In depth analysis of glide source code
猜你喜欢

巨杉数据库荣获艾媒咨询2022年中国信创产业双项荣誉

Getting started with webrtc: 11 In kurento, rtendpoint is used to pull RTP streams for clustering in live broadcast

Recommend a virtual machine software for fast cluster building of M1 chip computers

Shutter & flame - tankcombat game console development (I)

Final of the 11th Blue Bridge Cup embedded design and development project

MySQL笔记

Fluentd is easy to get started. Combined with the rainbow plug-in market, log collection is faster

2022-6-21os review group linking method

0007 reverse integer

Arcpy 添加图层到地图文档
随机推荐
Fluentd is easy to get started. Combined with the rainbow plug-in market, log collection is faster
SiCf batch activation service node
AcWing 241 楼兰图腾(树状数组详解)
Set up your own website (5)
Stop using system Currenttimemillis() takes too long to count. It's too low. Stopwatch is easy to use!
MySQL notes
leetcode 85. 最大矩形
SAP SPRO configure how to display the corresponding t-code
access_token不到两个小时失效的处理办法
MySQL_数据处理之查询
巨杉数据库受邀出席鲲鹏开发者年度盛会2022,共建国产化数字底座
Detailed explanation of rules and ideas for advance sale of deposit
MySQL 5.7 + Navicat 下载安装教程(附安装包)
Latex Greek alphabet
请问Flink的动态表是这样创建吗?我用flink cdc 读mysql数据,写flink动态表,发
助力金融信息化创新,巨杉数据库近期持续中标50余家金融客户
Sub account of bank payment interface development
leetcode 32. 最长有效括号
JAXB元素详解
Redis