ReverseBits

eclipse没问题,leetcode 1不能通过,超出int最大值了,但是怎么转无符号?

/*Write a function that takes an unsigned integer and returns the number of ’1‘ bits it has (also known as the Hamming weight).

For example, the 32-bit integer ’11‘ has binary representation 00000000000000000000000000001011, so the function should return 3.
*/

public static long reverseBits(int n) {
     long sum=0;
    List<Character> al=new ArrayList<Character>();
    String str = Long.toBinaryString(n);
    char[] ch = str.toCharArray();
    for(int i=0;i<32-ch.length;i++)
        al.add(‘0‘);
    for (int i = 0; i < ch.length; i++)
        al.add(ch[i]);
    System.out.println(al);
    for(int i=31;i>=0;i--) {
        if(al.get(i)==‘1‘)
        sum+=Math.pow(2,i);}
    return sum;
    }
时间: 2024-07-30 20:30:17

ReverseBits的相关文章

[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

位运算操作

来源:https://discuss.leetcode.com/topic/50315/a-summary-how-to-use-bit-manipulation-to-solve-problems-easily-and-efficiently/2 WIKI Bit manipulation is the act of algorithmically manipulating bits or other pieces of data shorter than a word. Computer p

Java [Leetcode 190]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). 解题思路: 移位操作. 代码如下: publi

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-190-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 as00111001011110000010100101000000). Follow up: If t

*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

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 func

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 Python 位操作 1

Python 位操作: 按位与 &, 按位或 | 体会不到 按位异或 ^ num ^ num = 0 左移 << num << 1 == num * 2**1 右移 >> num >> 2 == num / 2**2 取反 ~ ~num == -(num + 1) 1. Single Number Given an array of integers, every element appears twice except for one. Find