一天一道LeetCode

本系列文章已全部上传至我的github,地址:ZeeCoder‘s Github

欢迎大家关注我的新浪微博,我的新浪微博

我的个人博客已创建,欢迎大家持续关注!

一天一道leetcode系列依旧在csdn上继续更新,除此系列以外的文章均迁移至我的个人博客

另外,本系列文章已整理并上传至gitbook,网址:点我进

欢迎转载,转载请注明出处!

(一)题目

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

(二)解题

题目大意:给定一个32位的无符整形数,将其按位反转。

解题思路:首先判断第i位上是否为1,如果为1,则表示反转后的数的31-i位上为1;如果为0,则跳过。

class Solution {
public:
    uint32_t reverseBits(uint32_t n) {
        uint32_t reBits = 0;
        uint32_t tmp = 1;
        uint32_t Bits = 2147483648;//Bits为10000000 00000000 00000000 00000000
        for(int i = 0 ; i < 32 ; i++){
            if((n&tmp)==tmp){//这一位上为1
                reBits+=Bits>>i;//Bits右移i位
            }
            tmp = tmp<<1;
        }
        return reBits;
    }
};

【一天一道Leetcode】#190.Reverse Bits的更多相关文章

  1. LeetCode 190. Reverse Bits (算32次即可)

    题目: 190. Reverse Bits Reverse bits of a given 32 bits unsigned integer. For example, given input 432 ...

  2. [LeetCode] 190. Reverse Bits 颠倒二进制位

    Reverse bits of a given 32 bits unsigned integer. Example 1: Input: 00000010100101000001111010011100 ...

  3. LeetCode 190. Reverse Bits (反转位)

    Reverse bits of a given 32 bits unsigned integer. For example, given input 43261596 (represented in ...

  4. [LeetCode] 190. Reverse Bits 翻转二进制位

    Reverse bits of a given 32 bits unsigned integer. For example, given input 43261596 (represented in ...

  5. Java for LeetCode 190 Reverse Bits

    Reverse bits of a given 32 bits unsigned integer. For example, given input 43261596 (represented in ...

  6. Leetcode 190. Reverse Bits(反转比特数)

    Reverse bits of a given 32 bits unsigned integer. For example, given input 43261596 (represented in ...

  7. Java [Leetcode 190]Reverse Bits

    题目描述: everse bits of a given 32 bits unsigned integer. For example, given input 43261596 (represente ...

  8. Leetcode 190 Reverse Bits 位运算

    反转二进制 class Solution { public: uint32_t reverseBits(uint32_t n) { uint32_t ans = ; ; i<; ++i,n &g ...

  9. 【LeetCode】190. Reverse Bits 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 解题方法 二进制字符串翻转 位运算 日期 题目地址:https://le ...

随机推荐

  1. 【USACO11NOV】牛的阵容Cow Lineup 尺取法+哈希

    题目描述 Farmer John has hired a professional photographer to take a picture of some of his cows. Since ...

  2. bzoj3825 NOI2017 游戏

    题目背景 狂野飙车是小 L 最喜欢的游戏.与其他业余玩家不同的是,小 L 在玩游戏之余,还精于研究游戏的设计,因此他有着与众不同的游戏策略. 题目描述 小 L 计划进行nn 场游戏,每场游戏使用一张地 ...

  3. BZOJ4079 [Wf2014]Pachinko

    完整题面: 设f(i,j)表示路径经过(i,j)这个点的概率,列出方程消元. 但暴力消元的复杂度是$O((nm)^3)$,注意每一次消元只会影响前后m个方程,所以我们可以对于第i行,只存[i-m,i+ ...

  4. 开发者说 | 使用Visual Studio Code编译、调试Apollo项目

    转载地址:https://mp.weixin.qq.com/s?__biz=MzI1NjkxOTMyNQ==&mid=2247484266&idx=1&sn=d6bcd4842 ...

  5. selenium登录163邮箱

    环境:windows8  python2.7+selenium+chrome 直接上脚本: # coding=utf-8from selenium import webdriverimport tim ...

  6. C语言 字符串 字符串处理操作 字符串与函数

    字符数组的定义和初始化 宏常量+1  强调了字符串的最大强度 推荐忽略长度的定义 不能对所指向的存储单元内容修改,除非是字符串数组的定义 因为指针变量指向的是字符串数组的值,可以被修改. 未初始化 字 ...

  7. LeetCode 2

    No1 Given a sorted array and a target value, return the index if the target is found. If not, return ...

  8. 41. First Missing Positive(困难, 用到 counting sort 方法)

    Given an unsorted integer array, find the first missing positive integer. For example, Given [1,2,0] ...

  9. SQL注入原理及绕过安全狗

    1.什么是SQL注入攻击 SQL注入攻击指的是通过构造特殊的输入作为参数插入到Web表单的输入域或页面请求的查询字符串,欺骗服务器执行恶意的SQL命令 http://www.xxx.com/list. ...

  10. 给定桩号获取纵断面中的高程值(c# for civil3d)

    通过civil3d提供的api,也就是纵断面Profile类提供的方法---public double ElevationAt(double station),就可以很轻松的获取纵断面对象某桩号处的高 ...