LeetCode(47)-Reverse Bits
2016-04-12 10:08
537 查看
题目:
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: Reverse Integer
思路:
题意:给定一个无符号数,把他的二进制位转换过来考虑首先把原数字右移,然后得到右移的数字,赋值给新数字,然后左移动
-
代码:
public class Solution { // you need treat n as an unsigned value public int reverseBits(int n) { int res = 0; for(int i = 0; i < 32; i++, n >>= 1){ res = res << 1 | (n & 1); } return res; } }
相关文章推荐
- LeetCode(47)-Reverse Bits
- Phabricator部署手册
- 从B树、B+树、B*树谈到R 树
- 常用正则表达式
- 移动UI设计-主要导航模式
- 判断两个字符串是不是互为anagrams
- NGUI在5.3打包失败问题
- 同班同学20年后 身家15亿与月薪5000元的区别
- 欢迎访问我的个人博客:www.aidandai.com
- ecshop前后台函数整理
- NGUI在5.3打包失败问题
- mac os x下的软件包管理神器---homebrew
- C#开发微信门户及应用(31)--微信语义理解接口的实现和处理
- Windows8 64安装Oracle 11g说明
- myeclipse 安装SVN插件
- /etc/passwd与/etc/shadow配置详解
- 2016年全球人工智能技术大会的“根”(Root)源自何处?
- python简史
- linux awk命令
- Openstack学习笔记(十)-Keystone学习笔记(一)