- Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.
Subject to introduce
You are given a 32-bit signed integer x that returns the result of reversing the numeric portion of x.
If the inverted integer exceeds the range of 32-bit signed integers [−231, 231 − 1], 0 is returned.
Assume that the environment does not allow storage of 64-bit integers (signed or unsigned).
Example 1:
Input: x = 123 Output: 321 Example 2:
Input: x = -123 Output: -321 Example 3:
Input: x = 120 Output: 21 Example 4:
Input: x = 0 Output: 0
Tip:
-231 <= x <= 231 – 1
The problem solving
Can cause overflows and need to take account of exceptions
1. Convert numbers to strings, and then loop concatenation and conversion. Extract the last digit step by step using %10 and /10
Public static int reverse(int x) {// convert to string s = string.valueof (x); String i = ""; While (x! = 0) {// Get the single digit I = I + string.valueof (x % 10); // x = x / 10; } String name = ""; [] split = i.split("-"); for(String value : split) { name = name + value; } / / when the parameter to 0, 0 (the above logic judgment for "") if (name. The length () = = 0) {name =" 0 ". } // if (s.taintains ("-")) {name = "-" + name; } // Overflow problems may occur, manually return try {return integer.valueof (name); } catch (Exception e) { return 0; }}Copy the code
2. The input parameter is converted to a string, which is converted to an int by StringBuild
String s = String.valueOf(x); StringBuilder StringBuilder = new StringBuilder(s); If (s.startswith ("-")) {return integer.valueof ("-"+new StringBuilder(s.substring(1)).reverse().toString()); }else { return Integer.valueOf(new StringBuilder(s).reverse().toString()); } }catch (Exception e){ return 0; }Copy the code
3. Improvement of Method 2
The positive and negative judgment is put as try to improve performance, and is changed to less than, and symbol control is carried out through flag
String s = String.valueOf(x); String value = s; Int flag = 1; // Negative flag = 1; // Negative flag = 1; // Negative flag = 1; If (x < 0) {flag = -1; value = s.substring(1); Return integer.valueof (new StringBuilder(value).reverse().toString())) * flag; }catch (Exception e){ return 0; }Copy the code
4. Directly use int conversion, to determine the parameter
But!!!!!! This judgment makes no sense; the int is already strong when it is saved, so it does not trigger an overflow.
Therefore, we need to return 0 whenever /10 overflows in the loop
int reverse = 0; while (x ! Reverse = reverse *10 + x % 10; // Reverse = reverse *10 + x % 10; x = x /10; Return x > -2147483648&&x < 2147483647? reverse : 0;Copy the code
The previous value is recorded by an intermediate value, compared by /10, if different, then overflow is indicated
int reverse = 0; while (x ! Int temp = reverse; Reverse = reverse *10 + x % 10; x = x / 10; // If (reverse /10! = temp) { return 0; 1999xxx reverses to 9xxxx1 return reverse;Copy the code