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;
    }
};
时间: 2024-10-01 05:26:12

leetcod--Reverse Bits的相关文章

[LeetCode]Reverse Bits

题目:Reverse Integer Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 Have you thought about this? Here are some good questions to ask before coding. Bonus points for you if you have already thought through thi

leetcode笔记:Reverse Bits

一. 题目描述 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). 二. 题目分析 题目的要求比较简单,输

*Reverse Bits

题目: 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). Follow up:If this funct

LeetCode 192:Reverse Bits

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). Follow up: If this function

LeetCode Reverse Bits 反置位值

题意:给定一个无符号32位整数,将其二进制形式左右反置,再以整型返回. 思路:循环32轮,将n往右挤出一位就补到ans的尾巴上. 1 class Solution { 2 public: 3 uint32_t reverseBits(uint32_t n) { 4 if( !n ) return 0; 5 uint32_t ans = 0; 6 int i; 7 for(i=0; i<32; i++ ) 8 { 9 ans <<= 1; 10 if( n & 1 ) 11 ans

190. Reverse Bits(leetcode)

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). Follow up:If this function

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

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). Follow up: If this function

190. Reverse Bits Java Solutin

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). Follow up:If this function i

LeetCode之“数学”:Reverse Integer &amp;&amp; Reverse Bits

1. Reverse Integer 题目链接 题目要求: Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 click to show spoilers. Have you thought about this? Here are some good questions to ask before coding. Bonus points for you if

Java for LeetCode 190 Reverse Bits

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). JAVA实现如下: public int revers