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 ...
随机推荐
- 183.Wood Cut【hard】
183.Wood Cut[hard] Given n pieces of wood with length L[i] (integer array). Cut them into small piec ...
- mysql命令:set sql_log_bin=on/off
对于数据库的操作,经常需要暂时停止对bin-log日志的写入,那就需要这个命令:set sql_log_bin=on/off 参考:dev.mysql.com/doc/refman/5.5/en/se ...
- Python的open函数
打开一个文件并向其写入内容 Python的open方法用来打开一个文件.第一个參数是文件的位置和文件名称.第二个參数是读写模式.这里我们採用w模式,也就是写模式.在这样的模式下,文件原有的内容将会被删 ...
- android 阿拉伯语下,图库中编辑运动轨迹图片,动画中会显示绿色的图片
alps/packages/apps/Camera/src/com/android/camera/FileSaver.java 1:import java.util.Locale; 2:modify ...
- Win10 Anaconda下TensorFlow-GPU环境搭建详细教程(包含CUDA+cuDNN安装过程)(转载)
win7(win10也适用)系统安装GPU/CPU版tensorflow Win10 Anaconda下TensorFlow-GPU环境搭建详细教程(包含CUDA+cuDNN安装过程) 目录 2.配置 ...
- PHP中插件机制的一种实现方案
这篇文章的出发点是我对插件机制的理解,及其在PHP中的实现.此方案仅是插件机制在PHP中的实现方案之一,写下来和大家分享,欢迎大家一起讨论. 插件,亦即Plug-in,是指一类特定的功能模块(通常由第 ...
- C++标准库之tuple
构造 构造函数 tuple的构造函数很普通,没啥说的. default (1) constexpr tuple();默认构造函数 copy / move (2) tuple (const tuple& ...
- python 函数 参数 (难点传入dict list)
--使用参数组可以传值列表,字典:格式 #-*-coding:utf-8-*- def func5(x,*s,**gs): print(x) print(s) print(gs) print '*'* ...
- 用Python和py2app写独立的Mac OS X 应用
文/lovexiaov(简书作者)原文链接:http://www.jianshu.com/p/afb6b2b97ce9著作权归作者所有,转载请联系作者获得授权,并标注“简书作者”. 前提 创建一个普通 ...
- Android开发:《Gradle Recipes for Android》阅读笔记(翻译)2.3——用Eclipse ADT导出App
问题: 想在一个已经存在的Eclipse ADT的项目中使用Gradle 解决方案: Eclipse ADT插件可以帮助生成Gradle文件 讨论: Eclipse的ADT插件是在2013年推出Gra ...