Reverse Integer, leetcodereverse

Source: Internet
Author: User

Reverse Integer, leetcodereverse

Reverse digits of an integer.

Example1: x = 123, return 321
Example2: x =-123, return-321

Note:
The input is assumed to be a 32-bit signed integer. Your function shocould return 0 when the reversed integer overflows.

Returns the integer of a number.

Example 1: x = 123, 321 is returned
Example 2: x =-123, returns-321

Note:

Assume that the input is a 32-bit signed integer. When the inverted integer overflows, your function should return 0.

My personal biggest disadvantage of LeetCode is that I can do it as soon as I come up. because English is easy to read without Chinese, many prompts are not noticed at all. If this question overflows, 0 is returned.

 

class Solution {    public int reverse(int x) {      boolean isAbove=true;        StringBuffer sBuffer=new StringBuffer();        int newX=x;        if (x < 0) {            isAbove = false;            x = Math.abs(x);        }        while (x > 0) {            sBuffer.append(x % 10);            x = x / 10;        }        if (newX!=0) {            try {                if (isAbove)                    return Integer.parseInt(sBuffer.toString());                else                    return -Integer.parseInt(sBuffer.toString());            } catch (Exception e) {                return 0;            }                    }        else             return 0;    }}

 

Next, let's review the value range of the Basic JAVA data types:

The value range of byte is-128 ~ 127, occupies 1 byte (to the power of-2 to the power of 2 to the power of-1)
The short value range is-32768 ~ 32767, occupies 2 bytes (the 15th power of-2 to the 15th power of 2-1)
The value range of int Is (-2147483648 ~ 2147483647), occupies 4 bytes (the 31 Power of-2 to the 31 power of 2-1)
The value range of long is (-9223372036854774808 ~ 9223372036854774807), occupies 8 bytes (63 power of-2 to 63 power of 2-1)

  

 

 

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.