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