LeetCode_Easy_471:Number Complement
LeetCode_Easy_471: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 101 (no leading zero bits), and its complement is 010. 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.
思路笔记
高效解
我能想到的直观思路,就是在二进制转十进制的时候将各位余数数值取反,然后再转换为十进制。
但是知道我看到了最优解:Java 1 line bit manipulation solution(仅仅一句话代码)
public int findComplement(int num) {
return ~num & ((Integer.highestOneBit(num) << 1) - 1);
}
收获:与非位运算
我们首先要理解位运算:
说明:
非:
非运算符用符号“~”表示,其运算规律如下:
如果位为0,结果是1,如果位为1,结果是0,也就是每位都取反了。
public static void main(String[] args)
{
int a=2;
System.out.println("a 非的结果是:"+(~a));
}
在上述代码中:结果是 10 —> 01
与:
与运算符用符号“&”表示,其使用规律如下:
两个操作数中位都为1,结果才为1,否则结果为0
public static void main(String[] args)
{
int a=129;
int b=128;
System.out.println("a 和b 与的结果是:"+(a&b));
}
在上述代码中:结果是 100101001&100101000—>100101000
然后我们总结一下一句话代码:
- 假设Num=5(101),我们求出其非值为(11111111111111111111111111111010)这是一个32位的值,可是我们只想要其后三位。
- Integer.highestOneBit(num) << 1) - 1,可以快速求出和其长度相同的且各位为1的值,即111。
- 两者求与,剩下的就是010。
LeetCode_Easy_471:Number Complement的更多相关文章
- 【leetcode】476. Number Complement
problem 476. Number Complement solution1: class Solution { public: int findComplement(int num) { //正 ...
- LeetCode——Number Complement
LeetCode--Number Complement Question Given a positive integer, output its complement number. The com ...
- LeetCode_476. Number Complement
476. Number Complement Easy Given a positive integer, output its complement number. The complement s ...
- 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 ...
- 476. Number Complement
题目 Given a positive integer, output its complement number. The complement strategy is to flip the bi ...
- 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 (java实现)
原题链接 https://leetcode.com/problems/number-complement/ 原题 Given a positive integer, output its comple ...
随机推荐
- string 的函数
string 有一个很好用到函数:substr(index). 去掉前index个字符.
- tomcat各目录下的作用
1.tomcat的主目录下的文件 bin目录主要是用来存放tomcat的命令,主要有两大类,一类是以.sh结尾的(linux命令),另一类是以.bat结尾的(windows命令). 重要: 很多环 ...
- iOS 学习笔记三【segmentedControl分段控制器详细使用方法】
在iOS开发过程中,分段控制器的使用频率还是蛮高的,下面是我写的一个简单的demo,大家可以把代码直接复制过去,就可以使用,ios9最新支持. // // ViewController.m // 03 ...
- iOS开发-常用第三方开源框架介绍
iOS开发-常用第三方开源框架介绍 图像: 1.图片浏览控件MWPhotoBrowser 实现了一个照片浏览器类似 iOS 自带的相册应用,可显示来自手机的图片或者是网络图片,可自动从网 ...
- flume A simple example
http://flume.apache.org/FlumeUserGuide.html A simple example
- Unity3D学习笔记——NGUI之Localization system
Localization system(国际化系统) 实现的就是用户选择不同的语言,切换我们游戏文字的显示. 一:创建一个CVS文件.可以用Google Docs, Excel等软件工具. 我这里用的 ...
- OpenCV学习笔记廿一:opencv_contrib模块
一,简介: 该库为新加入代码库的算法.
- odata配置控制器方法路由1
查看edmx:http://localhost:12769/odata/$metadata 1.配置 ODataConventionModelBuilder builder = new ODataCo ...
- libnids介
转自:http://blog.chinaunix.net/uid-22832715-id-2111578.html Libnids开发包介绍 Libnids是一个用于网络入侵检测开发的专业编程 ...
- 1804 小C的多边形
1804 小C的多边形 基准时间限制:1 秒 空间限制:131072 KB 分值: 40 难度:4级算法题 小C偶然发现了一个奇妙的n个点的多边形.现在你需要给外圈的边标记上1~n-1,里圈的边也标记 ...