Title: Leetcode 007 Reverse Interger
Test instructions: Reverses the number of an integer. Retains the positive and negative symbols.
Idea: First the integer into a string, then determine whether it is a negative number, or whether it contains ' + ', and then start from the end of the string to accumulate a new integer can be.
However, there are special cases in which the forward is in the int range, but it overflows after the reversal and therefore is subject to a specific contract.
The code is as follows:
1 classSolution {2 Public:3 intReverseintx) {4 intLen, flag =1, i =0;5 Long LongAns =0;6 strings =to_string (x);7 8Len =s.size ();9len--;Ten if(S[i] = ='-') One { AFlag =-1; -i++; - } the //else if (s[i] = = ' + ') i++; - - while(Len >=i) - { +Ans *=Ten; -Ans + = s[len--]-'0'; + } AAns *=Flag; at //cout << ans << endl; - //The return value is automatically changed according to the preset function type, and the Longlong is automatically handled by int overflow - if(Ans > Int_max | | ans < int_min)return 0; - returnans; - } -};
"Leetcode" 007 Reverse Interger