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:

  1. The given integer is guaranteed to fit within the range of a 32-bit signed integer.
  2. 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

 然后我们总结一下一句话代码:

  1.     假设Num=5(101),我们求出其非值为(11111111111111111111111111111010)这是一个32位的值,可是我们只想要其后三位。
  2. Integer.highestOneBit(num) << 1) - 1,可以快速求出和其长度相同的且各位为1的值,即111。
  3. 两者求与,剩下的就是010。

LeetCode_Easy_471:Number Complement的更多相关文章

  1. 【leetcode】476. Number Complement

    problem 476. Number Complement solution1: class Solution { public: int findComplement(int num) { //正 ...

  2. LeetCode——Number Complement

    LeetCode--Number Complement Question Given a positive integer, output its complement number. The com ...

  3. LeetCode_476. Number Complement

    476. Number Complement Easy Given a positive integer, output its complement number. The complement s ...

  4. LeetCode#476 Number Complement - in Swift

    Given a positive integer, output its complement number. The complement strategy is to flip the bits ...

  5. LeetCode 476. Number Complement

    Given a positive integer, output its complement number. The complement strategy is to flip the bits ...

  6. 476. Number Complement

    题目 Given a positive integer, output its complement number. The complement strategy is to flip the bi ...

  7. Number Complement

    Given a positive integer, output its complement number. The complement strategy is to flip the bits ...

  8. LeetCode 476. Number Complement (数的补数)

    Given a positive integer, output its complement number. The complement strategy is to flip the bits ...

  9. 【LeetCode】476. Number Complement (java实现)

    原题链接 https://leetcode.com/problems/number-complement/ 原题 Given a positive integer, output its comple ...

随机推荐

  1. 60. Search Insert Position 【easy】

    60. Search Insert Position [easy] Given a sorted array and a target value, return the index if the t ...

  2. windows system.exe占用文件

    1)问题的原因是出于一个服务Application Experience,如果装好系统后就把此服务设为手动启动了,平时运行也没什么异常.但是win7在运行exe时如果没有这个服务的辅助就会长时间的占用 ...

  3. iOS开发UITableViewCell的选中时的颜色设置(转)

    iOS开发UITableViewCell的选中时的颜色设置   1.系统默认的颜色设置 //无色 cell.selectionStyle = UITableViewCellSelectionStyle ...

  4. Intent跳转系统的应用

    1.从google搜索内容    Intent intent = new Intent();    intent.setAction(Intent.ACTION_WEB_SEARCH);    int ...

  5. iframe详解

    如何查看是否为iframe *使用FireFox组件firebug->firepath 1.Top Window:可直接定位 2.iframe#i:根据id定位 定位方法: switch_to. ...

  6. ApiDoc 和 Swagger 接口文档

    ApiDoc:https://blog.csdn.net/weixin_38682852/article/details/78812244 Swagger git: https://github.co ...

  7. Android开发:《Gradle Recipes for Android》阅读笔记(翻译)2.6——签署发布apk

    问题: 为了将APK发布到google市场,需要对APK数字签名. 解决方案: 可以使用java的keytoll命令去创建一个证书,并且在gradle配置文件的signingConfigs块使用. 讨 ...

  8. linux压缩、解压缩命令

    tar -c: 建立压缩档案 -x:解压 -t:查看内容 -r:向压缩归档文件末尾追加文件 -u:更新原压缩包中的文件 这五个是独立的命令,压缩解压都要用到其中一个,可以和别的命令连用但只能用其中一个 ...

  9. 【BZOJ4974】字符串大师 KMP

    [BZOJ4974]字符串大师 Description 一个串T是S的循环节,当且仅当存在正整数k,使得S是T^k(即T重复k次)的前缀,比如abcd是abcdabcdab的循环节.给定一个长度为n的 ...

  10. 《从零开始学Swift》学习笔记(Day 19)——函数参数传递

    原创文章,欢迎转载.转载请注明:关东升的博客   函数的语法格式如下: func 函数名(参数列表) -> 返回值类型 { 语句组 return 返回值 } 关键字是func. 多个参数列表之间 ...