Leetcode "math": Reverse Integer && Reverse Bits

Source: Internet
Author: User
Tags bitset

1. Reverse Integer

Topic links

Title Requirements:

Reverse digits of an integer.

EXAMPLE1:X = 123, return 321
example2:x = -123, return-321

  Click to show spoilers.

Has a thought about this?

Here is some good questions to ask before coding. Bonus points for if you have already thought through this!

If the last digit are 0, what should the output being? ie, cases such as 10, 100.

Did you notice that the reversed integer might overflow? Assume the input is a 32-bit integer and then the reverse of 1000000003 overflows. How should handle such cases?

For the purpose of this problem, assume a your function returns 0 when the reversed integer overflows.

This problem is mainly to pay attention to the end of 0 and cross-border problems. The procedure is as follows:

1 classSolution {2  Public:3     intReverseintx) {4         if(x = =int_min)5             return 0;6             7         intnum =ABS (x);8         intLast =0;9         Longresult =0; Ten          while(num! =0) One         { Alast = num%Ten; -result = result *Ten+Last ; -Num/=Ten; the         } -          -         if(Result >Int_max) -             return 0; +          -         return(X >0) ? Result:-result; +     } A};
2. Reverse Bits

Topic links

Title Requirements:

Reverse bits of a given the unsigned integer.

For example, given input 43261596 (represented in binary as 00000010100101000001111010011100), return 964176192 (represent Ed in binary as00111001011110000010100101000000).

Follow up:
If This function is called many times, what would you optimize it?

Related Problem:reverse Integer

Credits:
Special thanks to @ts for adding this problem and creating all test cases.

The BITSET data structure is used in the procedure below, and it is important to note thatBits[0] is the most important.

1 classSolution {2  Public:3 uint32_t reversebits (uint32_t N) {4uint32_t one =1;5bitset< +>bits;6         inti = to;7          while(n! =0&& i >-1)8         {9Bits[i] = n &One ;Tenn = n >>1; Onei--; A         } -          -         returnBits.to_ulong (); the     } -};

Leetcode "math": Reverse Integer && Reverse Bits

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.