My O(1) solution for java


  • 1
    O

    this is my solution

      public class Solution {
        // you need treat n as an unsigned value
        public int reverseBits(int n) {
            int i=0;
            int reverseVal =0;
            while(i<=31){
                int temp=1<<i;
               
                int bitNum=temp&n;
                
                if(bitNum!=0){
                    reverseVal=(reverseVal<<1)+1;
                }else{
                    reverseVal=reverseVal<<1;
                }
                
                i++;
            
            }
            
            return reverseVal;
        }
    }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.