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)的更多相关文章

  1. 【leetcode】Reverse Integer(middle)☆

    Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 总结:处理整数溢出 ...

  2. 【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 ...

  3. 【leetcode】Next Permutation(middle)

    Implement next permutation, which rearranges numbers into the lexicographically next greater permuta ...

  4. 【leetcode】Word Break (middle)

    Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separa ...

  5. 【LeetCode】Counting Bits(338)

    1. Description Given a non negative integer number num. For every numbers i in the range 0 ≤ i ≤ num ...

  6. 【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 ...

  7. 【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 ...

  8. 【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 ...

  9. 【leetcode】Rotate Image(middle)

    You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise). ...

随机推荐

  1. ASP.NET 大文件下载的实现思路及代码

    文件下载是一个网站最基本的功能,ASP.NET网站的文件下载功能实现也很简单,但是如果遇到大文件的下载而不做特殊处理的话,那将会出现不可预料的后果.本文就基于ASP.NET提供大文件下载的实现思路及代 ...

  2. Linux运维初级教程(二)账户与安全

    知识点 用户ID为UID,组ID为GID,UID=0表示超级管理员即root. 一个用户只可以加入一个基本组,但是可以同时加入多个附加组. 创建用户时,系统默认会自动创建同名的组,并设置用户加入该基本 ...

  3. Maven初级学习(二)Maven使用入门

    序,学习配置pom.xml,利用maven生成eclipes项目. 一.编写POM POM Project Obejct Model,项目对象模型. 编写pom.xml,新建文件夹hello-worl ...

  4. 繁华模拟赛day8 字典序

    /* 这个题要我们求一个字典序,字符串给出的顺序,会对字母的字典序前后相对顺序进行限定,如何用来表示这种限定,我们注意到这种一个之后接着一个,只有先输出他前面的才能输出他,很明显就是拓扑排序,最小方案 ...

  5. block 块函数

    定义模块函数: <?php function smarty_block_text($args,$content,$smarty,$a) { $color=$args["color&qu ...

  6. php 调用系统命令

    system 与 exec 两者区别与联系:都会返回最后一行,命令执行成功的return返回值, 区别:system直接将输出内容echo出来,而exec将每一行输出内容保存到数组$output里. ...

  7. 在spring容器中定义初始化和销毁bean前所做的操作,有三种方式

    1.使用注解,通过@PostConstruct 和 @PreDestroy 方法 实现初始化和销毁bean之前进行的操作 package com.luoq.test.annotation.init; ...

  8. statusbar 样式

    1:statusBar字体为白色 在plist里面设置View controller-based status bar appearance 为 NO:设置statusBarStyle 为 UISta ...

  9. 流媒体(音频 AudioStreamer)

    AudioStreamer 在github可以搜索到, 专门播放流媒体(音频)

  10. 表单select相关

    selectedIndex 属性可设置或返回下拉列表中被选选项的索引号. options[] 返回包含下拉列表中的所有选项的一个数组. add()向下拉列表添加一个选项. blur()从下拉列表移开焦 ...