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 is called many times, how would you optimize it?

这个问题就是一个数字移位和赋值的问题,每次取出源操作数的最低位赋给目标操作数,并且源操作数右移一位,目标操作数左移一位。

class Solution {
public:
   uint32_t reverseBits(uint32_t n)
	{
		uint32_t iRet = 0;
		uint32_t iRes = n;

		for(int i = 0; i != 32; ++i)
		{
		    iRet <<= 1;
		    iRet = iRet | iRes & 1;
		    iRes >>= 1;
		}
		return iRet;
	}
};

  学习了c++的移位运算,c++还是弱的不行,慢慢学,不放弃,总会有改变的!!!

时间: 2024-10-17 21:16:15

LeetCode 192:Reverse Bits的相关文章

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

Leetcode 344:Reverse String 反转字符串(python、java)

Leetcode 344:Reverse String 反转字符串 公众号:爱写bug Write a function that reverses a string. The input string is given as an array of characters char[]. Do not allocate extra space for another array, you must do this by modifying the input array in-place wit

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

Leetcode solution 190: Reverse Bits

Problem Statement Reverse bits of a given 32 bits unsigned integer. Example 1: Input: 00000010100101000001111010011100 Output: 00111001011110000010100101000000 Explanation: The input binary string 00000010100101000001111010011100 represents the unsig

算法: Reverse Bits 反转位收藏

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

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

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

LeetCode OJ 190题:Reverse Bits

题目分析:常规解法,实在不知道如何优化了. 1 class Solution { 2 public: 3 uint32_t reverseBits(uint32_t n) 4 { 5 uint32_t m = 0; 6 for (uint32_t i = 1; i != 0; i <<= 1) 7 { 8 m <<= 1; 9 if (n & 1) 10 { 11 m |= 1; 12 } 13 n >>= 1; 14 } 15 16 return m; 17

Leetcode题目:Reverse Vowels of a String

题目: Write a function that takes a string as input and reverse only the vowels of a string. Example 1: Given s = "hello", return "holle". Example 2: Given s = "leetcode", return "leotcede". 题目解答: 要求将字符串中所有的元音字母逆转,辅音字