LeetCode Reverse Bits

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;
	}
}


阅读更多
文章标签: java leetcode Binary
个人分类: 算法/oj
上一篇LeetCode Rotate Array
下一篇LeetCode Number of 1 Bits
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭