My accepted simple solution in C++,Any advising?


  • 1
    K
    class Solution {
    public:
        uint32_t reverseBits(uint32_t n) 
        {
             unsigned int ret=0;
            int flag=1;
           // while(n)
            for(int i=0;i<32;i++)//1
            {
                flag=1;
                flag=flag&n;  //0
                n=n>>1;  //0
                ret=ret<<1; //10
                ret=ret|flag;//10
            }
            return ret;
        }
    };

  • 0
    G

    unsigned int ret=0;
    int flag=1;
    // while(n)
    for(int i=0;i<32;i++)//1
    {
    flag=1;
    flag=flag&n; //0
    n=n>>1; //0
    ret=ret<<1; //10
    ret=ret|flag;//10
    }
    return ret;


  • 0
    S
    class Solution:
        # @param n, an integer
        # @return an integer
        def reverseBits(self, n):
            origin = bin(n).lstrip('0b').zfill(32)
            now = int(origin[::-1],2)
            return now
    

    61ms


Log in to reply
 

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