LeetCode Reverse Bits

原创 2015年07月08日 21:00:37

Description:

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).

Solution:


import java.util.*;

public class Solution {
	public int reverseBits(int n) {
		String s = Integer.toBinaryString(n);
		n = 0;
		int token = 1;

		for (int i = 0; i < s.length(); i++) {
			n = n | (s.charAt(i) - '0') * token;
			token <<= 1;
		}
		for (int i = s.length(); i < 32; i++) {
			n <<= 1;
		}
		return n;
	}
}


Leetcode - Reverse Bits

Reverse bits of a given 32 bits unsigned integer.For example, given input 43261596 (represented in b...

[LeetCode]Reverse Bits

QuestionReverse bits of a given 32 bits unsigned integer.For example, given input 43261596 (represen...
  • byamao1
  • byamao1
  • 2016年12月13日 20:15
  • 91

[LeetCode][190][Reverse Bits]

题目链接:

【LeetCode】Reverse Bits 翻转二进制

题目Reverse Bits Reverse bits of a given 32 bits unsigned integer. For example, given input ...

leetcode笔记:Reverse Bits

题目的要求比较简单,输入一个32位的无符号整数,根据其二进制表示,输出与其二进制相对称的无符号整数。题目也给出了一个例子。...

LeetCode(190) Reverse Bits

题目如下: Reverse bits of a given 32 bits unsigned integer. For example, given input 43261596 (represe...

【LeetCode-190】 Reverse Bits(C++)

题目要求:要求将一个32位无符号整数所对应的二进制位反转得到的无符号整数。 解题思路:一般这种对应于二进制位的题目要考虑位运算!!而我可能对于位运算不太熟悉,每次都是用最愚蠢的办法做,虽然AC了,但是...

字节的按位逆序 Reverse Bits

http://www.hookcn.org/2011/01/reverse-bits.html   源自某公司的一道试题,问题很简单: 输入一个字节(8 bits),将其按位反序。 也就是说如...

190. Reverse Bits\331. Verify Preorder Serialization of a Binary Tree

Reverse Bits description implementation Verify Preorder Serialization of a Binary Tree description i...

190. Reverse Bits [easy] (Python)

题目链接https://leetcode.com/problems/reverse-bits/题目原文 Reverse bits of a given 32 bits unsigned integ...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:LeetCode Reverse Bits
举报原因:
原因补充:

(最多只允许输入30个字)