位运算(6)——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.
Example 1:
Input: 5
Output: 2
Explanation: The binary representation of 5 is (no leading zero bits), and its complement is . So you need to output 2.Example 2:
Input: 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.
public class Solution {
public int findComplement(int num) {
int mask = (Integer.highestOneBit(num) << 1) - 1;
num = ~num;
return num & mask;
}
}
Integer.highestOneBit(num); //01101返回1000,001返回1,101返回100
为什么需要mask:
例如num = 0000 1010,那么 ~num = 1111 0101,加入mask = 0000 1111后,~num前面的1都消除了。
位运算(6)——Number Complement的更多相关文章
- 【leetcode】Single Number && Single Number II(ORZ 位运算)
题目描述: Single Number Given an array of integers, every element appears twice except for one. Find tha ...
- 136.137.260. Single Number && 位运算
136. Single Number 意思就是给你一堆数,每个数都出现了两次,只有一个数只出现了一次,找出这个数 位运算(和c艹一样) &:按位与 |:按位或 ^:异或(一样为0,不一样为1) ...
- HDU 3006 The Number of set(位运算 状态压缩)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3006 题目大意:给定n个集合,每个集合都是由大于等于1小于等于m的数字组成,m最大为14.由给出的集合 ...
- LeetCode - 136. Single Number - ( C++ ) - 解题报告 - 位运算思路 xor
1.题目大意 Given an array of integers, every element appears twice except for one. Find that single one. ...
- The number of set(位运算+状态dp)一道十分美妙的题目哦!!!!!!
Given you n sets.All positive integers in sets are not less than 1 and not greater than m.If use the ...
- LeetCode 476. Number Complement
Given a positive integer, output its complement number. The complement strategy is to flip the bits ...
- LeetCode_Easy_471:Number Complement
LeetCode_Easy_471:Number Complement 题目描述 Given a positive integer, output its complement number. The ...
- 【LeetCode】位运算 bit manipulation(共32题)
[78]Subsets 给了一个 distinct 的数组,返回它所有的子集. Example: Input: nums = [,,] Output: [ [], [], [], [,,], [,], ...
- 【LeetCode】476. 数字的补数 Number Complement
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:Leetcode, 力扣,476, 补数,二进制,Pyth ...
- js中的位运算
按位运算符是把操作数看作一系列单独的位,而不是一个数字值.所以在这之前,不得不提到什么是"位": 数值或字符在内存内都是被存储为0和 1的序列,每个0和1被称之为1个位,比如说10 ...
随机推荐
- 开启和查看mysql的bin-log日志
[root@VM_0_7_centos data]# vim /etc/my.cnf [root@VM_0_7_centos data]# vim /etc/my.cnf [root@VM_0_7_c ...
- js去重方法
function remove(array){ var obj={}; newarray=[]; for(var i in array){ console.log(i); var arg=array[ ...
- 11、C内存四区模型
转载于:https://blog.csdn.net/wu5215080/article/details/38899259 内存四区模型 图1.内存四区模型流程说明1.操作系统把物理硬盘代码load到内 ...
- C++_友元3-其他友元关系
友元和类的关系还可以更复杂. 举个例子,假设出现了交互式遥控器,交互式遥控器能够让您回答电视节目中的问题,如果回答错误,电视将在控制器上产生嗡嗡声. 这个例子的问题,可以使用新的友元关系来解决.我把它 ...
- ABP中文网入门篇教程中的一个bug
入门--从空项目开始--使用ASP.NET Core Web Application https://cn.abp.io/documents/abp/latest/Autofac-Integratio ...
- BZOJ - 1941 最远估价
题意:求给出的所有点的最远点减最近点的最小差值 KD树的最远估价和最近估价略微不同,直接找最远垂线,反正xjb改一下就过了 #include<bits/stdc++.h> #define ...
- 深入理解promise
如今promise大行其道,关于异步方面的几乎都有它的影子,新的fetch api返回的是promise对象,generator中的yield后面一般也会跟promise对象,async的await后 ...
- C++ STL之Set
set是关联容器,类似于集合. 特点是里面的元素不会重复,而且元素时有序的. 1.声明定义: #include<set> using namespace std; set<int&g ...
- HDU4499
In Chinese Chess, there is one kind of powerful chessmen called Cannon. It can move horizontally or ...
- Union Find - 20181102 - 20181105
Union Find: 589. Connecting Graph public class ConnectingGraph { //父节点数组 private int[] father = null ...