Leetcode:reverse Integer

Source: Internet
Author: User

A dropped last night before bedtime. The problem is still very water.

Title Description: Given an int number, you will flip the output later.

Tip: The problem itself is very simple, and it is important to think about the situation in a comprehensive way. For example: 1, int number reversal After the problem of overflow. 2, the end of the number of 0, after flipping how to output the problem, such as 100.

Code:

public class Solution {    final static int max = 2147483647;    Final static int testmax = 214748364;    Final static int min = -2147483648;    Final static int testmin = -214748364;    public int reverse (int x) {        int sum = 0;        Boolean FLG = false;        if (x<0)            FLG = true;        while (x!=0) {        if (!FLG) {        if (sum > Testmax | | (Sum==testmax && (x%10) >7))        return 0;        }        else {        if (Sum < Testmin | | (Sum==testmin && (x%10) <-8))        return 0;        }            sum*=10;            sum+= (x%10);            x/=10;                    }        return sum;    }}


This problem also has a solution is to use the array, the code is slightly higher, but conducive to understanding. The following stickers I use the array implementation of the code (in my classmate based on the change), more ugly, will see it.

public class Solution {public    int reverse (int x) {        long sum = 0;        Boolean FLG = false;        if (x<0)            FLG = true;//negative        String str = string.valueof (x);        char[] Strarray = Str.tochararray ();        for (int i = (flg?1:0); i < strarray.length; i++) {        sum + = (strarray[i]-' 0 ') *math.pow (FLG? ( I-1): i);        }        if (FLG)        sum = -1*sum;        if (sum>=integer.min_value && sum<=integer.max_value)        return (int) sum;        else        return 0;}    }



This problem reminds me of the interview the other day, the interviewer let C + + implement the Atoi function, a string into the int type.

Function prototype: int atoi (char* str)

This may have to consider the completeness of the problem more, welcome to discuss, back to the implementation of this.

Leetcode:reverse Integer

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.