【leetcode】Reverse Bits(middle)
Reverse bits of a given 32 bits unsigned integer.
For example, given input 43261596 (represented in binary as 00000010100101000001111010011100), return 964176192 (represented in binary as00111001011110000010100101000000).
思路:
n一直右移, ans一直左移。
class Solution {
public:
uint32_t reverseBits(uint32_t n) {
uint32_t ans = ;
for(int i = ; i < ; i++)
{
if(n & 0x0001 == )
{
n = n >> ;
ans = ans << ;
ans |= 0x0001;
}
else
{
n = n >> ;
ans = ans << ;
}
}
return ans;
}
};
大神总会有更简单的写法:
uint32_t reverseBits(uint32_t n) {
uint32_t result= ;
for(int i=; i<; i++)
result = (result<<) + (n>>i &);
return result;
}
【leetcode】Reverse Bits(middle)的更多相关文章
- 【leetcode】Reverse Integer(middle)☆
Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 总结:处理整数溢出 ...
- 【leetcode】Reorder List (middle)
Given a singly linked list L: L0→L1→…→Ln-1→Ln,reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→… You must do thi ...
- 【leetcode】Next Permutation(middle)
Implement next permutation, which rearranges numbers into the lexicographically next greater permuta ...
- 【leetcode】Word Break (middle)
Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separa ...
- 【LeetCode】Counting Bits(338)
1. Description Given a non negative integer number num. For every numbers i in the range 0 ≤ i ≤ num ...
- 【leetcode】Rotate List(middle)
Given a list, rotate the list to the right by k places, where k is non-negative. For example:Given 1 ...
- 【leetcode】Partition List(middle)
Given a linked list and a value x, partition it such that all nodes less than x come before nodes gr ...
- 【leetcode】Spiral Matrix(middle)
Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral or ...
- 【leetcode】Rotate Image(middle)
You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise). ...
随机推荐
- 通过Canvas及File API缩放并上传图片完整示例
<!DOCTYPE html> <html> <head> <title>通过Canvas及File API缩放并上传图片</title> ...
- 基于zepto判断mobile的横竖屏状态
借用jquery mobile中的代码,删除了一些多余的部分,简单的基于zepto的模块 var CheckOrientation = (function(){ var win = $( window ...
- [设计模式] javascript 之 迭代子模式
迭代子模式:定义 迭代子模式,又称游标模式,是一种用于对聚集进行顺序访问规则的模式,是一种行为模式:它用于提供对聚集对象的一种统一的访问接口,使客户能够在不了解聚集对象内部结构的情况对聚集对象进行访问 ...
- UML之用例图
用例图概要 ²用例图是被称为参与者的外部用户所能观察到的系统功能的模型图. (<UML参考手册>) ²用例图列出系统中的用例和系统外的参与者,并显示哪个参与者参与了哪个用例的执行 (或称为 ...
- ie下获取上传文件全路径
ie下获取上传文件全路径,3.5之后的火狐是没法获取上传文件全路径的 /*获取上传文件路径*/ function getFilePath(obj) { var form = $(this).paren ...
- Hadoop 之Impala
impala 是基于hive的大数据实时分析查询引擎,直接使用Hive的元数据库metadata意味着impala元数据都存储在hive的metadstore中并且impala兼容hive的 sql解 ...
- I’ve seen the world,lit it up as my stage now
I've seen the world,lit it up as my stage now 阅尽繁华 点亮红尘做舞台 Channeling angels in,the new age now 粉末登场 ...
- 【PHP面向对象(OOP)编程入门教程】16.__toString()方法
我们前面说过在类里面声明“__”开始的方法名的方法(PHP给我们提供的),都是在某一时刻不同情况下自动调用执行的方 法,“__toString()”方法也是一样自动被调用的,是在直接输出对象引用时自动 ...
- Redis学习笔记十:独立功能之监视器
通过执行 monitor 命令可以让客户端自己变成一个监视器,实时接收并打印当前处理的命令请求的相关信息. 127.0.0.1:6379> monitor OK 1451752646.83727 ...
- 关于viewport
最近无聊的很,买了本教材,学习响应式网站设计. 因为有多年css的编程基础,前面的媒介查询学的很顺利.当学到viewport这个mata标签的时候,教程讲的比较简单. 今天,百度了不少资料,基本搞清楚 ...