Leetcode 2 --- Reverse Integer, reverseinteger
Question:
Looking for a job near graduation, although I have read the data structure and algorithms, I feel confused. In addition, the programming mentioned in the project is based on improved algorithms from other aspects. In short, my programming skills are limited and often despised. Therefore, I made up my mind to crawl sloWly and click leetcode a little bit, this is also the reason for writing these blogs, recording the analysis ideas and solutions when encountering problems.
---------- I walked slowly, but I never stepped back.
========================================================== ========================================================== ====================================
Question:
Reverse digits of an integer.
Example1: x = 123, return 321
Example2: x =-123, return-321
----
Ignorance:
Do not write programs when you see this question. Why? Because you have not considered it carefully. If you write it up, you will find many situations in the process of writing the program and it will be stuck there. It should be analyzed, mainly including the following points;
1. If the end is 0, how can the output be changed? For example, 10,100,
2. please pay attention to overflow, because int type has its boundary value no matter whether there are symbols or not, it will overflow when the boundary value is exceeded, for example, 1000000003, the returned result is 0 when the false overflow of this topic
From the analysis point of view, this question focuses on how to deal with overflow, so for an integer, we can easily obtain numbers from everyone, but how can we determine whether to reverse it to overflow, I have tried several methods for a long time, but I think the most reliable is the following idea. For an integer, whether it is positive or negative, if it does not overflow after the flip, the number before the flip should be the same as the number after the flip, such as: Before the flip 123, 321 after flip. If no overflow occurs, the number on the highest digit after flip must be equal to the number on the second digit before flip. If the number on the highest digit is different, an overflow occurs during the flip process, you can directly return 0 for processing. If you understand this truth, you can refer to the following code for beginners:
/*************************************** * *********************************> File Name: reverseInteger. cc> Author: Jerry Shi> Mail: jerryshi0110@gmail.com> Created Time: ******************************** **************************************** /# include <iostream> # include <cstdlib> using namespace std; class Solution {public: int reverse (int x) {int sign = (x <0 )? -1: 1; x = abs (x); char buf [12]; // max length of int is 10int I = 0; int j = 0; int result = 0; int temp; // get signwhile (x) {buf [I] = x % 10; x/= 10; result = result * 10 + buf [I]; + + I;} temp = result; while (I) {if (temp % 10! = Buf [I-1]) {return 0;} temp/= 10; -- I;} return sign * result ;}; int main (void) {int test = 1534236469; solution mtest; int result; result = mtest. reverse (test); cout <"inverse result:" <result <endl; return 0 ;}After the code is submitted, the test passes and the test result takes 60 ms. The above code can be simpler, but the solution is the same as the main idea. Later, I looked at some of the best methods in the discussion board, basically using the above analysis method to determine whether the overflow exists. Another method is to use an int64 variable to receive the result after the flip, then compare it with INT_MAX and INT_MIN to determine whether the overflow exists. This method can solve the problem in this case, but it will not work if uint64 is given.