Leetcode: 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.
Better solution:
- public static int highestOneBit(int i)
int
value with at most a single one-bit, in the position of the highest-order ("leftmost") one-bit in the specified int
value.
- public class Solution {
- public int findComplement(int num) {
- return ~num & ((Integer.highestOneBit(num) << 1) - 1);
- }
- }
一般方法:
- public class Solution {
- public int findComplement(int num) {
- int res = 0;
- int i = 31;
- while (i >= 0) {
- if (((num >>> i) & 1) == 1) break;
- i--;
- }
- while (i >= 0) {
- if (((num >>> i) & 1) == 0) {
- res |= 1<<i;
- }
- i--;
- }
- return res;
- }
- }
Leetcode: Number Complement的更多相关文章
- LeetCode——Number Complement
LeetCode--Number Complement Question Given a positive integer, output its complement number. The com ...
- [LeetCode] Number Complement 补数
Given a positive integer, output its complement number. The complement strategy is to flip the bits ...
- 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
problem 476. Number Complement solution1: class Solution { public: int findComplement(int num) { //正 ...
- LeetCode_476. Number Complement
476. Number Complement Easy Given a positive integer, output its complement number. The complement s ...
- 2016.5.15——leetcode:Number of 1 Bits ,
leetcode:Number of 1 Bits 代码均测试通过! 1.Number of 1 Bits 本题收获: 1.Hamming weight:即二进制中1的个数 2.n &= (n ...
- LeetCode——Number of Boomerangs
LeetCode--Number of Boomerangs Question Given n points in the plane that are all pairwise distinct, ...
- LeetCode_Easy_471:Number Complement
LeetCode_Easy_471:Number Complement 题目描述 Given a positive integer, output its complement number. The ...
- LeetCode 476. Number Complement (数的补数)
Given a positive integer, output its complement number. The complement strategy is to flip the bits ...
随机推荐
- excel数据导入mysql
先把excel数据另存成txt文件 Load Data InFile 'D:/1.txt' Into Table `res_type_collect` fields terminated by '@‘ ...
- C语言中使用bool
声明 C语言中是没有bool类型的. 使用方法 参考: https://stackoverflow.com/q/1921539.
- Beta(4/7)
鐵鍋燉腯鱻 项目:小鱼记账 团队成员 项目燃尽图 冲刺情况描述 站立式会议照片 各成员情况 团队成员 学号 姓名 git地址 博客地址 031602240 许郁杨 (组长) https://githu ...
- PostgreSQL自学笔记:3 数据库的基本操作
3 数据库的基本操作 3.1 创建数据库 3.1.1 使用对象浏览器创建数据库 [Server] -> PostgreSQL 9.6 -> 数据库,右击 -> 创建 通常: 数据库: ...
- 洛谷.3733.[HAOI2017]八纵八横(线性基 线段树分治 bitset)
LOJ 洛谷 最基本的思路同BZOJ2115 Xor,将图中所有环的异或和插入线性基,求一下线性基中数的异或最大值. 用bitset优化一下,暴力的复杂度是\(O(\frac{qmL^2}{w})\) ...
- jdk1.8.0_40 +maven+tomcat7.0+mysql8.0详细安装教程
(一) jdk的安装 1.下载jdk推荐下载地址:http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-213 ...
- Qt写websocketpp服务端
1.下载websocketpp,地址为https://github.com/zaphoyd/websocketpp,版本为0.7. 2.下载boost,地址为https://www.boost.org ...
- Spark on Yarn with HA
Spark 可以放到yarn上面去跑,这个毫无疑问.当Yarn做了HA的时候,网上会告诉你基本Spark测不需做太多的关注修改,实际不然. 除了像spark.yarn开头的相关配置外,其中一个很重要的 ...
- js一些代码
1判断金额正则 var reg = /(^[1-9]([0-9]+)?(\.[0-9]{1,2})?$)|(^(0){1}$)|(^[0-9]\.[0-9]([0-9])?$)/; var money ...
- 请求库之requests模块
本片导航: 介绍 基于GET请求 基于POST请求 响应Response 高级用法 一.介绍 #介绍:使用requests可以模拟浏览器的请求,比起之前用到的urllib,requests模块的a ...