Lc7- integer reversal

/**
 * Given a 32-bit signed integer, you need this integer numbers on each inverted.
 * 
 * Example 1:
 * 
 * Input: 123 Output: 321 Example 2:
 * 
 * Input: Output -123: -321 Example 3:
 * 
 * Input: 120 Output: 21 Note:
 * 
 * Suppose we have an environment can store the 32-bit signed integer, then the value range of [-231 231--1]. Please According to this hypothesis, if integer overflow after reverse it returns 0.
 * 
 * Source: stay button (LeetCode) link: https: //leetcode-cn.com/problems/reverse-integer
 * All the copyright collar button network. Commercial reprint please contact the authorized official, non-commercial reprint please indicate the source.
 *
 */
public class Lc7 {
    public static int reverse(int x) {
        boolean negativeNumberFlag = false;
        if (x < 0) {
            negativeNumberFlag = true;
            x *= -1;
        }

        int len = 0;
        int temp = x;
        while (temp > 0) {
            temp /= 10;
            len++;
        }

        int[] array = new int[len];
        for (int i = 0; i < len; i++) {
            array[i] = (int) (x % Math.pow(10, i + 1) / Math.pow(10, i));
        }
        long digital = 0;
        for (int i = array.length - 1, j = 0; i >= 0; i--, j++) {
            digital += array[i] * Math.pow(10, j);
        }
        digital = negativeNumberFlag == true ? digital * -1 : digital;

        return digital > Integer.MAX_VALUE || digital < Integer.MIN_VALUE ? 0 : (int) digital;
    }

    public static int reverse1 ( int X) {
         // · Extraction of the positive and negative numbers n 1 - 1 Negative
         int  Sign  = X > =  0 ? 1 : - 1 ;

        // · digitally reversed
        S String = String.valueOf (X);
         // · minus be considered in digital, the need to exclude the
         IF ( Sign  ==  - . 1 ) {
            s = s.substring(1);
        }
        char CH []  = s.toCharArray ();
         int  len  = ch.length;
         // · Analyzing the array is odd or even bit inversion algorithm to select a different
         IF ( len  %  2  ==  0 ) {
             int MID =  len  /  2  -  . 1 ;
             for ( int I = MID, J = MID +  . 1 ; I > =  0 ; I - , J ++) { 
                char  TEMP  = CH [i];
                ch[i] = ch[j];
                ch[j] = temp;
            }
        } else {
            int mid = len / 2;
            for (int i = 1; (i + mid) < len; i++) {
                char temp = ch[mid - i];
                ch[mid - i] = ch[mid + i];
                ch[mid + i] = temp;
            }
        }

        // · exclude start with numbers 0
        s = String.valueOf(ch);
        int intercept = s.indexOf('0');
        if (intercept != -1) {
            s.substring(0, intercept);
        }

        long res = sign * Long.parseLong(s);
        return (res > Integer.MAX_VALUE || res < Integer.MIN_VALUE) ? 0 : (int) res;

    }

    public static void main(String[] args) {
        int digit = -123;
//        System.out.println(reverse(digit));
        System.out.println(reverse1(digit));
    }
}

Guess you like

Origin www.cnblogs.com/xiaoshahai/p/12152659.html