leetcod--Reverse Bits

2023-12-20 11:18
文章标签 reverse bits leetcod

本文主要是介绍leetcod--Reverse Bits,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

题目描述:

everse bits of a given 32 bits unsigned integer.

For example, given input 43261596 (represented in binary as 00000010100101000001111010011100), return 964176192 (represented in binary as00111001011110000010100101000000).

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

Related problem: Reverse Integer

Credits:
Special thanks to @ts for adding this problem and creating all test cases.

解题思路:

将一个整形数的二进制序列逆转,后输出二进制序列代表的十进制数

方法一:

采用一个32长度的数组,存储二进制,之后逆转,再计算此数组所表示的整形数。

class Solution {
public:uint32_t reverseBits(uint32_t n) {vector<int> a(32,0);int mood=0,i=0,temp;unsigned int temp1=0;while(n/2!=0){mood=n%2;a[i]=mood;n/=2;i++;}a[i]=n%2;reverse(a.begin(),a.end());for(int j=0;j<32;j++){if (a[j]!=0){temp=pow(2*1.0,j);temp1+=temp;}}return temp1;}
};

方法二:

利用位运算符,从低位到高位取出原整形数的各个比特位,对应转换为0~31,1~30,...,31~0。用31-当前下标 就是转换之后的下标,在用左移操作即可实现二进制到十进制的转换。
class Solution {
public:uint32_t reverseBits(uint32_t n) {uint32_t bin=0;for (int i = 0; i < 32; i++) bin+=(n >> i & 1)<<(31-i);return bin;}
};





这篇关于leetcod--Reverse Bits的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/515842

相关文章

leetcode#541. Reverse String II

题目 Given a string and an integer k, you need to reverse the first k characters for every 2k characters counting from the start of the string. If there are less than k characters left, reverse all of

[LeetCode] 7. Reverse Integer

题:https://leetcode.com/problems/reverse-integer/description/ 题目 Given a 32-bit signed integer, reverse digits of an integer. Example 1: Input: 123Output: 321Example 2:Input: -123Output: -321Ex

[LeetCode] 338. Counting Bits

题:https://leetcode.com/problems/counting-bits/description/ 题目 Given a non negative integer number num. For every numbers i in the range 0 ≤ i ≤ num calculate the number of 1’s in their binary repres

[LeetCode] 190. Reverse Bits

题:https://leetcode.com/problems/reverse-bits/ 题目大意 将32位的数,二进制翻转。 解题思路 解法和 将int a =123,翻转的思路 相同。 int b= 0;while(a>0){b = b*10 + a %10;a /=10;} 将新数整体左移一位,然后 取每个数的第i位置,将该位放到 新数的最低位。循环32次遍可以得到翻转。

《python语言程序设计》第8章第11题将反向字符串 编写一个函数反向一个字符串,reverse(s)

def reverse(text_arrange):len_text = len(text_arrange)dec_text = ""for i in range(1, len_text + 1):# print(i)dec_text += text_arrange[-i]print(f"反向输出{dec_text}")reverse("12345678")reverse("abcdefg

leetcode338 Counting Bits Java

Description Given a non negative integer number num. For every numbers i in the range 0 ≤ i ≤ num calculate the number of 1’s in their binary representation and return them as an array. Example: Fo

[CTF]-Reverse:Reverse做题笔记

Tea: [HNCTF 2022 WEEK2]TTTTTTTTTea: 找出关键数据,运行脚本 #include <stdio.h>int main(){unsigned int l,r;unsigned int v4[6]={-1054939302,-1532163725,-165900264,853769165,768352038,876839116};int flag[6]={0};

Leetcode139: Reverse Linked List II

Reverse a linked list from position m to n. Do it in-place and in one-pass. For example: Given 1->2->3->4->5->NULL, m = 2 and n = 4, return 1->4->3->2->5->NULL. Note: Given m, n satisfy the fol

第七题:整数反转(Reverse Integer)

题目描述: 给定一个 32 位有符号整数 x,返回其反转后的整数。反转后的整数可能会超出 32 位整数的范围,因此需要注意处理溢出情况。 示例: 输入:x = 123 输出:321 输入:x = -123 输出:-321 输入:x = 120 输出:21 输入:x = 0 输出:0 要求: 需要考虑反转后的整数是否会超出 32 位整数的范围。32 位有符号整数的范围是 ([-2^

win7 x64 PL/SQL连接Make sure you have the 32 bits Oracle Cient italled.问题解决

一、当前环境 oracle11g win7 x64 PLSQL Developer 9.0.0.1601 使用plsql登录出现提示 Initialization error Could not initialize "F:\app\Administrator\product\11.2.0\dbhome_ 1\bin\oci... Make sure you have the 32