LeetCode-Reverse Bits, 1 Bit is related to the binary state of the number, leetcodereverse

Source: Internet
Author: User

LeetCode-Reverse Bits, 1 Bit is related to the binary state of the number, leetcodereverse

Https://leetcode.com/problems/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 as0011100101111000001010010000000 ).

The simple method is to traverse it, but this method is very inefficient and seems to have no better way.

class Solution {public:    uint32_t reverseBits(uint32_t n) {        if(0 == n)        {            return 0;        }        int res = 0;        for(int i = 0; i < 32; i++)        {            if( n & 1)            {                res += (1 << (31-i));            }            n = n >> 1;        }        return res;    }};

Number of 1 Bits

Https://leetcode.com/problems/number-of-1-bits/

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 representation00000000000000000000000000001011, So the function shocould return 3.

Note that n & (n-1) can delete the last 1, so you don't have to traverse it all. How many 1 is there and how many times it loops?

class Solution {public:    int hammingWeight(uint32_t n) {        if(0 == n)        {            return 0;        }        int res = 0;        while(n)        {            n = n&(n-1);            res++;        }        return res;    }};


Reverse Integer

Https://leetcode.com/problems/reverse-integer/

Reverse digits of an integer.

Example1: x = 123, return 321
Example2: x =-123, return-321 here the calculation process is not complex, but be sure to exceed the range

Class Solution {public: int reverse (int x) {long xx = x; // prevent negative numbers from exceeding Fan Wen if (0 = x) {return 0 ;} int flag = 0; if (x <0) {flag = 1; xx =-x;} stack <int> temp; while (xx) {temp. push (xx % 10); xx = xx/10;} long index = 1; long res = 0; while (! Temp. empty () {res + = temp. top () * index; index = index * 10; temp. pop () ;}if (flag) {res = res * (-1); if (res <INT_MIN) return 0 ;}if (res> INT_MAX) {return 0 ;} return res ;}};


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.