1. Title
- Give a 32-bit signed integer
- Invert this integer, the number on each
- Assume that the environment does not allow the storage of 64 bit integers (signed or unsigned).
[external chain picture transfer failed. The source station may have anti-theft chain mechanism. It is recommended to save the picture and upload it directly (img)-
2. Array
Pre knowledge, understanding is enough
- It is a collection of data of the same type. Once the array is initialized, its length cannot be changed. Therefore, it is necessary to specify the initial capacity
- Is a physically continuous storage structure, that is, the array name represents the first address of the continuous space
- Through the first address, all elements of the array can be accessed in turn, and the subscript reads the data under the time complexity of O(1)
3. Problem solving ideas
-
Reverse order
-
Head and tail exchange
-
Mathematical thinking solution
1. Violent solution (reverse order)
-
step
- int to String, String to char []
- The bottom layer of the array is actually a char [], and from JDK1.9, it is: byte []
- Traverses the character array in reverse and stores the elements in the new array
- Convert the new array into a string and then into an integer for output
- int to String, String to char []
-
Boundaries and details:
-
Boundary:
-
Array Index Overflow
-
Value overflow boundary: [− 2 ^ 31, 2 ^ (31 − 1)], overflow returns 0
- It can be seen that the absolute value of the minimum value is 1 greater than the maximum value. MIN and MAX cannot be reversed
-
-
Details:
- 32 bits, i.e. int type
- The first is not 0
-
-
code:
public static int reverse(int x) { // Note: after the absolute value is taken as the minimum value of Integer, it must be 1 greater than the maximum value, so it cannot be reversed if (x == Integer.MIN_VALUE || x == Integer.MAX_VALUE) { return 0; } // Processing positive and negative numbers int sign = x > 0 ? 1 : -1; // Get symbol // Take absolute value x = x < 0 ? -x : x; String str = String.valueOf(x); // int to String char[] charArray = str.toCharArray(); // String to char [] // Traverse the array in reverse, and then store the elements in the new array // 1. New array initialization int num = charArray.length; char[] newChar = new char[num]; // 2. Reverse traversal for (int i = 0; i < num; i++) { newChar[i] = charArray[(num - 1) - i]; } // The new array is converted to String and then int String newStr = String.valueOf(newChar); // To String // Not all strings can be converted to integers. Therefore, convert them to long before judging long value = Long.parseLong(newStr); // Judge that the overflow is 0 boolean b = value > Integer.MAX_VALUE || value < Integer.MIN_VALUE; int result = b ? 0 : (int) value; return result * sign; // Do symbol processing }
2. End to end exchange
-
Steps:
-
int to String, String to char []
-
Loop traversal: perform head and tail exchange (until there are 1 or 0 operations left in the array)
- Start > = end, the cycle ends and the inversion is completed
-
Finally, convert char [] to string and string to int
-
-
code:
public static int reverse(int x) { // Boundary judgment if (x == Integer.MAX_VALUE || x == Integer.MIN_VALUE) return 0; // Positive and negative number processing int sign = x < 0 ? -1 : 1; // Extract negative sign x = x < 0 ? -x : x; // Get absolute value String str = String.valueOf(x);// To String char[] charArray = str.toCharArray();// To char [] // Swap the first (start) and last (end) digits int start = 0, end = charArray.length - 1; while (start < end) { // Swap equidistant elements at both ends char temp = charArray[start]; charArray[start] = charArray[end]; charArray[end] = temp; start++; end--; } String newStr = String.valueOf(charArray); // Reverse result to String long value = Long.parseLong(newStr); // String to long (some numbers will overflow the int range after being reversed) // Determine the number greater than 32 (0 if overflow) int result = value > Integer.MAX_VALUE || value < Integer.MIN_VALUE ? 0 : (int) value; return result * sign; // Do symbol processing }
3. Mathematical thinking solution( ⭐)
It is also the optimization solution among the three solutions
-
Steps:
- Take the mold and get the lowest position
- Take the quotient again and remove the lowest order
- Loop traversal, the lowest bit obtained is accumulated and spliced
-
Details:
- At the highest position:
- x%10 = x
- x/10 = 0
- (both conditions can be adjusted as the end of the cycle, just choose one)
- The spliced integer may overflow the range of int. use long to store it. Finally, judge that the overflow int returns 0
- The first is not 0
- Symbol processing. (extract the minus sign to get the result, and then restore)
- At the highest position:
-
code:
public static int reverse3(int x) { // The boundary value of int cannot be reversed (the absolute value difference is 1) if (x == Integer.MAX_VALUE || x == Integer.MIN_VALUE) return 0; // Symbol processing int sign = x < 0 ? -1 : 1; // Get symbol x = x < 0 ? -x : x; // Gets the absolute value (both positive and negative are treated as positive numbers) long sum = 0; // result int last; // Get the lowest order while ((last = x % 10) != x) { // The lowest bit = = itself, indicating that it is the last bit sum = sum * 10 + last; x /= 10; // Remove lowest bit } // At the end of the cycle, last is the highest bit (spliced into the result) if (last != 0) { sum = sum * 10 + last; // Overflow processing sum = sum > Integer.MAX_VALUE ? 0 : sum; } return (int) sum * sign; // Positive and negative processing }
- The mathematical thinking solution discards the data structure, saves memory and improves efficiency