当前位置:网站首页>Leetcode- complement of numbers - simple
Leetcode- complement of numbers - simple
2022-06-13 05:49:00 【AnWenRen】
title :476 The complement of numbers - Simple
subject
Negates the binary representation of an integer (0 change 1 ,1 change 0) after , Then convert to decimal representation , You can get the complement of this integer .
for example , Integers 5 The binary representation of is “101” , Take the reverse and get “010” , Then turn back to decimal to get the complement 2 .
Give you an integer num , Output its complement .
Example 1
Input :num = 5
Output :2
explain :5 The binary representation of 101( No leading zeros ), The complement is 010. So you need to output 2 .
Example 2
Input :num = 1
Output :0
explain :1 The binary representation of 1( No leading zeros ), The complement is 0. So you need to output 0 .
Tips
1 <= num < 231
Code Java
public int findComplement(int num) {
// 1. Find the high position first 1
int highbit = 0;
for (int i = 31; i >= 0; i--) {
if (((num >> i) & 1) != 0){
highbit = i;
break;
}
}
// 2. For high position all 1 To engage in exclusive or have to result
int result = highbit == 30 ? 0x7fffffff : (1 << (highbit + 1)) - 1 ;
return result ^ num;
}
边栏推荐
- ffmpeg 下载后缀为.m3u8的视频文件
- How to Algorithm Evaluation Methods
- Leetcode- reverse vowels in string - simple
- 13 cancelendevent of a flowable end event and compensationthrowing of a compensation event
- Unity游戏优化[第二版]学习记录6
- How to Algorithm Evaluation Methods
- 2021-9-19
- Bicolor case
- Etcd fast cluster building
- The reason why the process cannot be shut down after a spark job is executed and the solution
猜你喜欢
Explanation of sentinel series' features, composition and deployment
Interrupt processing
Mongodb multi field aggregation group by
MongoDB 多字段聚合Group by
16 the usertask of a flowable task includes task assignment, multi person countersignature, and dynamic forms
Building a stand-alone version of Nacos series
Config server configuration center of Nacos series
C calls the API and parses the returned JSON string
2021.9.30 learning log -postman
ArrayList loop removes the pit encountered
随机推荐
Tongweb adapts to openrasp
Working principle of sentinel series (concept)
Leetcode- intersection of two arrays ii- simple
A fast week
2020 personal annual summary
Unity游戏优化(第2版)学习记录7
Compilation croisée helloworld avec cmake
High availability of Nacos series
Celery understands
Conf/tongweb Functions of properties
Django uploads local binaries to the database filefield field
Parallelgateway and exclusivegateway of 14 gateways
Solution to prompt "permission is required to perform this operation" (file cannot be deleted) when win10 deletes a file
Exception after repeated application redeployment on tongweb: application instance has been stopped already or outofmemoryerror:metaspace
2021.9.29 learning log restful architecture
Nacos series registry principle and source code analysis
Explanation of sentinel series' features, composition and deployment
AUTOSAR实战教程pdf版
The 13th week of the second semester of sophomore year
Vagrant virtual machine installation, disk expansion and LAN access tutorial