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 ...
随机推荐
- docker下部署spring boot
第 5 章 Docker + Spring Boot: 快速搭建和部署Java Web应用 0.你需要: JDK 1.8 : java -version Maven 3.0+ : mvn -v Git ...
- C++中两个类相互include的问题
在构造自己的类时,有可能会碰到两个类之间的相互引用问题,例如:定义了类A类B,A中使用了B定义的类型,B中也使用了A定义的类型 例如: Cup.h #ifndef CUP_H #define CUP_ ...
- oracle中查看sql语句的执行计划
1.在pl/sql中打开cmd命令容器 2.在cmd命令窗口中输入:explain plan for select * from t; 3.查看sql语句的执行计划:select * from tab ...
- C++获取二维数组的元素个数
C/C++获取二维数组的大小/长度/元素个数 ][]; ]) /
- tomcat遇到版本问题
1.Dynamic web module 2.J2EE Web modules 3.Tomcat version 4.<web-app xmlns:xsi="http://www.w ...
- 也许,这样理解HTTPS更容易_转载
转自:也许,这样理解HTTPS更容易 原文衔接:https://showme.codes/2017-02-20/understand-https/ 作者:翟志军 摘要 本文尝试一步步还原HTTPS的 ...
- 谈谈 epmd
在<Erlang/OTP 并发编程实战>中,对 epmd 有如下描述: epmd 代表 Erlang 端口映射守护进程(Erlang Port Mapper Daemon). 每启动一个 ...
- 前端webview开发中遇到的一些问题及其解决方法
最近做了一个webview中的兑换页面,本来以为很简单,想不到遇到了远远超出预期数量的BUG,记下来,以备后患. 1 inline-block元素折行 BUG描述:现在我有三个DIV,要在一列等宽排列 ...
- tonymillion/Reachability的使用
tonymillion/Reachability是GitHub上的一个开源工具类,目測是依据Apple的Reachability Demo改写而成. 该类能够測试到某一网络.主机等的可达性,支持Blo ...
- css 定位标签设置格式
td a { color: #3c8dbc; } td a:hover { color: #00bdd8; } 上例即为定位td下的a标签.即用来给表格中的链接,未访问时和hove ...