当前位置: 首页 > news >正文

闯关leetcode——190. Reverse Bits

大纲

  • 题目
    • 地址
    • 内容
  • 解题
    • 代码地址

题目

地址

https://leetcode.com/problems/reverse-bits/description/

内容

Reverse bits of a given 32 bits unsigned integer.

Note:

  • Note that in some languages, such as Java, there is no unsigned integer type. In this case, both input and output will be given as a signed integer type. They should not affect your implementation, as the integer’s internal binary representation is the same, whether it is signed or unsigned.
  • In Java, the compiler represents the signed integers using 2’s complement notation. Therefore, in Example 2 above, the input represents the signed integer -3 and the output represents the signed integer -1073741825.

Example 1:

Input: n = 00000010100101000001111010011100
Output: 964176192 (00111001011110000010100101000000)
Explanation: The input binary string 00000010100101000001111010011100 represents the unsigned integer 43261596, so return 964176192 which its binary representation is 00111001011110000010100101000000.

Example 2:

Input: n = 11111111111111111111111111111101
Output: 3221225471 (10111111111111111111111111111111)
Explanation: The input binary string 11111111111111111111111111111101 represents the unsigned integer 4294967293, so return 3221225471 which its binary representation is 10111111111111111111111111111111.

Constraints:

  • The input must be a binary string of length 32

Follow up: If this function is called many times, how would you optimize it?

解题

这题就是要将二进制进行逆序。解法也很简单,就是逐位判断是否为1,然后使用“或”操作,将其放置到对应位置。我们不一定需要对32位都进行判断,只要其退位到0时,我们就可以终止探索。

#include <cstdint>class Solution {
public:uint32_t reverseBits(uint32_t n) {uint32_t result = 0;int offset = -1;while (n != 0) {offset++;if (n & 0x80000000) {result |= 1 << offset;}n <<= 1;}return result;}
};

在这里插入图片描述

代码地址

https://github.com/f304646673/leetcode/tree/main/190-Reverse-Bits/cplusplus


http://www.mrgr.cn/news/56668.html

相关文章:

  • Jmeter 实战 JDBC配置
  • 智慧产权,专业守护 —— 全影知产专利保护机构
  • FC<PropsWithChildren<Props>>:React 函数组件的类型定义
  • HarmonyOS Next应用开发——图像PixelMap变换
  • 一文详细讲解CRM系统(附架构图、流程、功能介绍)
  • 深度学习(一)基础:神经网络、训练过程与激活函数(1/10)
  • [论文笔记]ColPali: Efficient Document Retrieval with Vision Language Models
  • PCL SAC-IA 算法实现点云粗配准(永久免费版)
  • 【卡尔曼滤波】观测模型包含输入的线性卡尔曼滤波
  • 输出时间序列中的时区是什么Series.dt.tz_convert(tz)
  • 酒店智能轻触开关的类型及其应用
  • 过零检测比较器电路设计
  • 【数据结构与算法】Java中的基本数据结构:数组、链表、树、图、散列表等。
  • Java | Leetcode Java题解之第502题IPO
  • Android Audio基础——音频混合器介绍(十二)
  • 深入解析 FarmHash 算法C++ 实现与性能优化
  • 【源码+文档】基于SpringBoot+Vue城市智慧社区综合服务平台【提供源码+答辩PPT+参考文档+项目部署】
  • 什么是感知与计算融合?
  • java中double强制转换int引发的OOM问题
  • 大厂物联网(IoT)高频面试题及参考答案
  • AIGC文本生成视频
  • Python中的isinstance和hasattr
  • 【使用Flask构建RESTful API】从零开始开发简单的Web服务!
  • 追寻数组的轨迹,解开算法的情愫
  • Python语法基础:复数
  • 【在Linux世界中追寻伟大的One Piece】Socket编程UDP