作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/


题目地址:https://leetcode.com/problems/reverse-bits/description/

题目描述

Reverse bits of a given 32 bits unsigned integer.

Example:

Input: 43261596
Output: 964176192
Explanation: 43261596 represented in binary as 00000010100101000001111010011100,
return 964176192 represented in binary as 00111001011110000010100101000000.

Follow up:

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

解题方法

二进制字符串翻转

python真的好。bin()能求出二进制表示,ine(str,2)能把二进制转十进制表示。注意这个题目里面一定要是32位的二进制表示才行,因此一定要进行补全。

class Solution:
# @param n, an integer
# @return an integer
def reverseBits(self, n):
bins = bin(n)[2:][::-1]
rev = (bins + '0' * (32 - len(bins)))
return int(rev, 2)

位运算

二刷使用C++,进行位运算。每次看n的末尾数字是多少,然后拼接到res的最前面。这样循环32次,则把n的所有数字进行了逆序操作。

class Solution {
public:
uint32_t reverseBits(uint32_t n) {
uint32_t res = 0;
for (int i = 0; i < 32; ++i) {
res = (res << 1) + (n & 1);
n >>= 1;
}
return res;
}
};

日期

2018 年 2 月 4 日
2018 年 11 月 27 日 —— 最近的雾霾太可怕

【LeetCode】190. Reverse Bits 解题报告(Python & C++)的更多相关文章

  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】120. Triangle 解题报告(Python)

    [LeetCode]120. Triangle 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址htt ...

随机推荐

  1. 60-Lowest Common Ancestor of a Binary Search Tree

    Lowest Common Ancestor of a Binary Search Tree My Submissions QuestionEditorial Solution Total Accep ...

  2. typedef定义数组

    typedef定义数组 问题来源 在学习高一凡数据结构与算法解析串这一章节时,遇到如下代码不明白其意义,经过查阅终于搞明白 typedef unsigned char SString[MAXLEN + ...

  3. 日常Java 2021/10/31

    泛型类 泛型类的声明和非泛型类的声明类似,除了在类名后面添加了类型参数声明部分.和迈型方法一样,泛型类的类型参数声明部分也包含一个或多个类型参数,参数间用逗号隔开.一个泛型参数,也被称为一个类型变量, ...

  4. act.四级

    act的词源是do, 干着或干了的事情也可以叫act.action: doing sth; act: n. action, v. do; activity: busy, energetic, or占据 ...

  5. iOS 客户端获取七牛上传token

    一.官方参考文档: 1.上传策略http://developer.qiniu.com/article/developer/security/put-policy.html 2.上传凭证(即uptoke ...

  6. java代码定时备份mysql数据库及注意事项——基于 springboot

    源码地址: https://gitee.com/kevin9401/BackUpDataBase git 拉取: https://gitee.com/kevin9401/BackUpDataBase. ...

  7. Linux学习 - 文件包处理命令

    一.搜索文件find find  [搜索范围]  [匹配条件] (1) -name(名字查找) <1>  find  /etc  -name  init 查找/etc下以 "in ...

  8. Linux基础命令---put上传ftp文件

    put 使用lftp登录ftp服务器之后,可以使用put指令将文件上传到服务器.   1.语法       put [-E]  [-a]  [-c] [-O base]  lfile  [-o rfi ...

  9. docker之镜像制作

    #:下载镜像并初始化系统 root@ubuntu:~# docker pull centos #:创建目录 root@ubuntu:/opt# mkdir dockerfile/{web/{nginx ...

  10. SpringCloud微服务-Eureka服务注册与发现

    一. Eureka 是什么? Eureka是Netflix的一个子模块,也是核心模块之一.Eureka是一个基于REST的服务,用于定位服务,以实现云端中间层服务发现和故障转移.服务注册与发现对微服务 ...