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 ...
随机推荐
- JavaScript之RegExp对象
ECMAScript 通过 RegExp 类型来支持正则表达式.使用下面类似 Perl 的语法,就可以创建一个正则表达式 var expression = / pattern / flags ; 其中 ...
- python模块学习之six模块
Six:Python 2和3兼容性库 Six提供了简单的实用程序,用于覆盖Python 2和Python 3之间的差异.它旨在支持在Python 2和3中都可以进行修改的代码库. 六个只包含一个Pyt ...
- C++加密解密库之选择
项目中有这样一个需求,客户端登陆服务器时,为保证信息安全,需要对用户的密码进行加密传输,在服务器端接受到之后进行相应的解密. 一.加密算法分类 对称加密算法.不对称加密算法.不可逆加密算法 1.对称加 ...
- stm32开发板无法正常写入的问题或者写入后无法正常运行的问题
在调试stm32的程序的时候遇到这样一个问题. 用Keil往stm32里写入程序时出现无法访问内存的错误.情况如下 然后 J-link没有提示异常.偶尔可以下载成功,但是程序无法正确的执行. 经过一番 ...
- 通过Get方式传递数据
1:因为get传参数有个特点就是不能超过256字节.如果数据大的话会溢出. 解决办法: $data=json_encode($data_array); 然后在拼接超链接: <a href=&qu ...
- java 调用cmd命令
public class Port{ public static void main(String[] args) { Runtime runtime=Runtime.getRuntime(); tr ...
- 图谱论(Spectral Graph Theory)基础
拉普拉斯矩阵(Laplacian matrix),也称为导纳矩阵(Admittance matrix)或者基尔霍夫矩阵(Kirchohoff matrix) 归一化的拉普拉斯矩阵定义为 例子: 拉普拉 ...
- PHP实现自己活了多少岁
1.mktime()函数的功能 2.代码: $birth = mktime(0,0,0,10,2,1992);//出生的时间戳 $time = time();//当前的时间戳 $age = floor ...
- 关于lucene的RAMDirectory和FSDirectory的性能问题的困惑
关于lucene的RAMDirectory和FSDirectory的性能问题的困惑 在lucene in Action书中说RAMDirectory的性能总是比FSDirectory优越(书中2.7. ...
- JS中的加号+运算符详解
加号+运算符 在 JavaScript 中,加法的规则其实很简单,只有两种情况: 把数字和数字相加 把字符串和字符串相加 所有其他类型的值都会被自动转换成这两种类型的值. 为了能够弄明白这种隐式转换是 ...