Java实现 LeetCode 190 颠倒二进制位
190. 颠倒二进制位
颠倒给定的 32 位无符号整数的二进制位。
示例 1:
输入: 00000010100101000001111010011100
输出: 00111001011110000010100101000000
解释: 输入的二进制串 00000010100101000001111010011100 表示无符号整数 43261596,
因此返回 964176192,其二进制表示形式为 00111001011110000010100101000000。
示例 2:
输入:11111111111111111111111111111101
输出:10111111111111111111111111111111
解释:输入的二进制串 11111111111111111111111111111101 表示无符号整数 4294967293,
因此返回 3221225471 其二进制表示形式为 10101111110010110010011101101001。
提示:
请注意,在某些语言(如 Java)中,没有无符号整数类型。在这种情况下,输入和输出都将被指定为有符号整数类型,并且不应影响您的实现,因为无论整数是有符号的还是无符号的,其内部的二进制表示形式都是相同的。
在 Java 中,编译器使用二进制补码记法来表示有符号整数。因此,在上面的 示例 2 中,输入表示有符号整数 -3,输出表示有符号整数 -1073741825。
进阶:
如果多次调用这个函数,你将如何优化你的算法?
public class Solution {
// you need treat n as an unsigned value
public int reverseBits(int n) {
int a=0;
for(int i=0;i<=31;i++){
// n >> i 一位一位来
// &= 1 取出最低位
// <<=(31 - i) 颠倒,左移到前面
a=a+((1&(n>>i))<<(31-i));
}
return a;
}
}
Java实现 LeetCode 190 颠倒二进制位的更多相关文章
- Leetcode 190.颠倒二进制位 By Python
颠倒给定的 32 位无符号整数的二进制位. 示例: 输入: 43261596 输出: 964176192 解释: 43261596 的二进制表示形式为 000000101001010000011110 ...
- leetcode 190. 颠倒二进制位(c++)
颠倒给定的 32 位无符号整数的二进制位. 示例 1: 输入: 00000010100101000001111010011100输出: 00111001011110000010100101000000 ...
- 力扣(LeetCode)颠倒二进制位 个人题解
颠倒给定的 32 位无符号整数的二进制位. 示例 1: 输入: 00000010100101000001111010011100 输出: 0011100101111000001010010100000 ...
- Java for LeetCode 190 Reverse Bits
Reverse bits of a given 32 bits unsigned integer. For example, given input 43261596 (represented in ...
- 【python】Leetcode每日一题-颠倒二进制位
[python]Leetcode每日一题-颠倒二进制位 [题目描述] 颠倒给定的 32 位无符号整数的二进制位. 示例1: 输入: 00000010100101000001111010011100 输 ...
- Java for LeetCode 216 Combination Sum III
Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...
- Java for LeetCode 214 Shortest Palindrome
Given a string S, you are allowed to convert it to a palindrome by adding characters in front of it. ...
- Java for LeetCode 212 Word Search II
Given a 2D board and a list of words from the dictionary, find all words in the board. Each word mus ...
- Java for LeetCode 211 Add and Search Word - Data structure design
Design a data structure that supports the following two operations: void addWord(word)bool search(wo ...
随机推荐
- 模板引擎 Thymeleaf 动态渲染 HTML
1.添加依赖 <!-- Thymeleaf 模板引擎 --> <dependency> <groupId>org.thymeleaf</groupId> ...
- FPGA自计数六位共阳极数码管动态显示2(调用task的方法)
`timescale 1ns/1ps module adc_dis( clk , rst_n , sm_seg , sm_bit ); input clk;//50HZ input rst_n; :] ...
- [hdu5254]BFS
题意:如果一个格子的相邻四个格子中存在两个格子被标记,且这两个格子有公共点,那么这个格子也被标记.给定初始的标记状态,求最终有多少个格子被标记了 思路: 依次对每个格子进行处理,看它能否”生成“新的被 ...
- HDU 2014 (水)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2014 题目大意:给你 n 个数,去掉 max 和 min ,求平均数 解题思路: 很水,边记录分数,边 ...
- HDU 2013 (水)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2013 题目大意:已知最后一天桃子只有一个,告诉你猴崽子第一天吃掉总桃子数的一半多一个,第二天吃掉剩下总 ...
- spring mvc json返回防止乱码
乱码问题 乱码一直是编程的常见问题,spring mvc 返回json数据时可能导致乱码,需要在controller中添加如下代码: @RequestMapping("/test" ...
- POI 导入excel数据自动封装成model对象--介绍
1.项目开发中,导入输入应该是常用的基本功能.我们经常会使用excel将数据导入到数据库,在导入之前必须得将excel数据转换成javaBean对象 2.由于此功能经常使用,所以开发此工具类方便日后轻 ...
- 对background: url("~assets/img/common/collect.svg") 0 0/14px 14px 的理解
需求:给收藏数字前面通过::before伪元素添加图标 相关代码: .goods-info .collect { position: relative; } .goods-info .collect: ...
- vue 3.0新特性
参考: https://www.cnblogs.com/Highdoudou/p/9993870.html https://www.cnblogs.com/ljx20180807/p/9987822 ...
- Rabbitmq 报错 nodedown
问题描述 线上 rabbitmq 集群账号密码设置的过于简单,有一定的风险.在整改过程中发现,三台机器信息错乱,每台服务器执行rabbitmq 相关的命令就报错,Error: unable to co ...