题目

https://leetcode-cn.com/problems/reverse-bits/

题解

方法1:JDK 自带的 Integer.reverse() 方法源码

    /*** Returns the value obtained by reversing the order of the bits in the* two's complement binary representation of the specified {@code int}* value.** @param i the value to be reversed* @return the value obtained by reversing order of the bits in the*     specified {@code int} value.* @since 1.5*/public static int reverse(int i) {// HD, Figure 7-1i = (i & 0x55555555) << 1 | (i >>> 1) & 0x55555555;i = (i & 0x33333333) << 2 | (i >>> 2) & 0x33333333;i = (i & 0x0f0f0f0f) << 4 | (i >>> 4) & 0x0f0f0f0f;return reverseBytes(i);}

方法2:常规解法

public class Solution {public static void main(String[] args) {Solution solution = new Solution();solution.reverseBits2(-3);}/*** 方法2:打败 100%*/public int reverseBits2(int n) {int res = 0;for (int i = 0; i < 32; i++) {res = (res << 1) + (n >>> i & 1);}return res;}/*** 方法1:通过数组*/public int reverseBits(int n) {//输入是一个十进制整数System.out.print(n);int[] bits = new int[32];int size = 0;for (int i = 31; i >= 0; i--) { // 十进制转二进制bits[size++] = n >>> i & 1;}System.out.println(" 转换为二进制:" + Arrays.toString(bits));int res = 0;for (int i = 31; i >= 0; i--) {//二进制转十进制res = ((res << 1) + (bits[i] & 1));}return res;}
}

方法3:打表

1、打 256 表
class Solution {private static final int[] lookup = new int[]{0, 128, 64, 192, 32, 160, 96, 224, 16, 144, 80, 208, 48, 176, 112, 240, 8, 136, 72, 200, 40, 168, 104, 232, 24, 152, 88, 216, 56, 184, 120, 248, 4, 132, 68, 196, 36, 164, 100, 228, 20, 148, 84, 212, 52, 180, 116, 244, 12, 140, 76, 204, 44, 172, 108, 236, 28, 156, 92, 220, 60, 188, 124, 252, 2, 130, 66, 194, 34, 162, 98, 226, 18, 146, 82, 210, 50, 178, 114, 242, 10, 138, 74, 202, 42, 170, 106, 234, 26, 154, 90, 218, 58, 186, 122, 250, 6, 134, 70, 198, 38, 166, 102, 230, 22, 150, 86, 214, 54, 182, 118, 246, 14, 142, 78, 206, 46, 174, 110, 238, 30, 158, 94, 222, 62, 190, 126, 254, 1, 129, 65, 193, 33, 161, 97, 225, 17, 145, 81, 209, 49, 177, 113, 241, 9, 137, 73, 201, 41, 169, 105, 233, 25, 153, 89, 217, 57, 185, 121, 249, 5, 133, 69, 197, 37, 165, 101, 229, 21, 149, 85, 213, 53, 181, 117, 245, 13, 141, 77, 205, 45, 173, 109, 237, 29, 157, 93, 221, 61, 189, 125, 253, 3, 131, 67, 195, 35, 163, 99, 227, 19, 147, 83, 211, 51, 179, 115, 243, 11, 139, 75, 203, 43, 171, 107, 235, 27, 155, 91, 219, 59, 187, 123, 251, 7, 135, 71, 199, 39, 167, 103, 231, 23, 151, 87, 215, 55, 183, 119, 247, 15, 143, 79, 207, 47, 175, 111, 239, 31, 159, 95, 223, 63, 191, 127, 255};public int reverseBits(int n) {int r = 0;for (int i = 0; i < 4; i++) {r = (r << 8) | lookup[n & 0xff];n >>= 8;}return r;}
}

2、打 65536 表

因为代码长度限制,所以 lookup 只能在创建类时计算得到,无法硬编码。

public class Solution {// you need treat n as an unsigned valueprivate static final int[] lookup = new int[65536];{for (int i = 0; i < 65536; i++) {lookup[i] = Integer.reverse(i) >>> 16;}}public int reverseBits(int n) {int r = 0;for (int i = 0; i < 2; i++) {r = (r << 16) | lookup[n & 0xffff];n >>= 16;}return r;}
}

leetcode 190. Reverse Bits | 190. 颠倒二进制位(移位操作,十进制二进制相互转换,打表法)相关推荐

  1. [勇者闯LeetCode] 190. Reverse Bits

    [勇者闯LeetCode] 190. Reverse Bits Description Reverse bits of a given 32 bits unsigned integer. For ex ...

  2. leetcode python3 简单题190. Reverse Bits

    1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第一百九十题 (1)题目 英文: Reverse bits of a given 3 ...

  3. LeetCode 190. Reverse Bits (算32次即可)

    题目: Reverse Bits Reverse bits of a given 32 bits unsigned integer. For example, given input 43261596 ...

  4. 190. Reverse Bits

  5. 【Leetcode】[190] 颠倒二进制位

    [Leetcode][190] 颠倒二进制位 Author: Xin Pan Date: 2022.3.13 题目 原题链接 颠倒给定的 32 位无符号整数的二进制位. 解法 考虑使用位运算来做,因为 ...

  6. leetcode#190 颠倒二进制位

    leetcode#190 颠倒二进制位 题目: 颠倒给定的 32 位无符号整数的二进制位. 示例: 输入: 00000010100101000001111010011100 输出: 001110010 ...

  7. java二进制反转_Java实现 LeetCode 190 颠倒二进制位

    190. 颠倒二进制位 颠倒给定的 32 位无符号整数的二进制位. 示例 1: 输入: 00000010100101000001111010011100 输出: 0011100101111000001 ...

  8. C#LeetCode刷题之#190-颠倒二进制位(Reverse Bits)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/4050 访问. 颠倒给定的 32 位无符号整数的二进制位. 输入: ...

  9. 20190903:(leetcode习题)颠倒二进制位

    颠倒二进制位 题目 大致思路 代码实现 题目 大致思路 思路一:将该数与1,10,100- 等32个数做异或运算,判断对应二进制数每一位的数字是0还是1,然后将其存入数组res,最后乘以对应的幂次,即 ...

最新文章

  1. PHP中绘制图像的一些函数总结
  2. 强化学习(四)---基于模型动态规划问题
  3. Python工程师具备哪些技能才能提升求职机率?
  4. 面试官问:ZooKeeper是强一致的吗?怎么实现的?
  5. 工作260:js判断一个数组是否包含一个指定的值
  6. 各自然带代表植被_植被垂直带谱?水与热之间的较量。
  7. python字符串用法_笔记:python字符串的使用
  8. java菜单管理的实现方式_智能停车场管理系统的收费实现方式有哪些?
  9. 关于CCSpriteSheet报错问题
  10. 人生三分之一的睡眠决定着另外三分之二的精彩
  11. [转移]今天做了的一些事
  12. html制作清明上河图
  13. Python搭建聊天机器人微信订阅号
  14. 【一起入门NLP】中科院自然语言处理作业二:中英文语料训练CBOW模型获得词向量(pytorch实现)【代码+报告】
  15. php 文本域,关于使用文本域(TextArea)的一个问题
  16. 某猫电影 css 加密解决方案
  17. Windows IIS SqlServer .Net/Asp.NET
  18. C语言源代码系列-管理系统之电子英汉词典
  19. 《计算之魂》思考题4.3
  20. jlink_v8原理图

热门文章

  1. POJ - 1696 Space Ant(极角排序)
  2. 几何基础之判断线段相交问题
  3. PyTorch-训练
  4. Spring MVC 入门--Hello World
  5. _MSC_VER详细介绍
  6. c++ template(8)模版多态
  7. 使用MAP文件快速定位程序崩溃代码行
  8. 计算机网络 | 应用层 :HTTP协议详解
  9. C++ STL : 模拟实现STL中的vector类
  10. 简历上的“熟练掌握 RPC”,到底是个什么水平?