博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
LeetCode-Reverse Bits
阅读量:4611 次
发布时间:2019-06-09

本文共 857 字,大约阅读时间需要 2 分钟。

Reverse bits of a given 32 bits unsigned integer.

For example, given input 43261596 (represented in binary as 00000010100101000001111010011100), return 964176192 (represented in binary as 00111001011110000010100101000000).

Follow up:

If this function is called many times, how would you optimize it?

Related problem:

Credits:

Special thanks to for adding this problem and creating all test cases.

Analysis:

Shift result to left by 1 bit, get the last bit of n, put the bit onto result.

Solution:

public class Solution {    // you need treat n as an unsigned value    public int reverseBits(int n) {        if (n==0) return 0;                int res = 0;        for (int i=0;i<32;i++){            res = (res << 1);            if ((n & 1)==1) res++;            n = n >> 1;        }        return res;    }}

 

转载于:https://www.cnblogs.com/lishiblog/p/5870335.html

你可能感兴趣的文章
有关implicit Intent的使用
查看>>
关于Android log拿不到的情况
查看>>
spring入门——applicationContext与BeanFactory的区别
查看>>
微服务架构
查看>>
在WPF中获取DataGridTemplateColumn模板定义的内容控件
查看>>
WPF疑难杂症之二(全屏幕窗口)
查看>>
Directx11教程(9) 增加一个TimerClass类
查看>>
Directx11教程(45) alpha blend(2)
查看>>
C# WPF 滚动字幕实现
查看>>
win10 uwp 毛玻璃
查看>>
[ 搭建Redis本地服务器实践系列一 ] :图解CentOS7安装Redis
查看>>
Wix 安装部署教程(七) 获取管理员权限
查看>>
sql server 备份与恢复系列二 事务日志概述
查看>>
visual studio 2017 添加MSDN
查看>>
Asp.Net SignalR Hub类中的操作详解
查看>>
《Programming WPF》翻译 第3章 3.内嵌控件
查看>>
ASP.NET MVC+EF框架+EasyUI实现权限管理系列(21)-用户角色权限基本的实现说明
查看>>
编写WCF服务时右击配置文件无“Edit WCF Configuration”(编辑 WCF 配置)远程的解决办法...
查看>>
CSS垂直居中总结
查看>>
快速构建Windows 8风格应用25-数据绑定
查看>>