LeetCode-Atoi, leetcode
Implement atoi to convert a string to an integer.
There are already many solutions available on the Internet.
Here we mainly pay attention to some points (the most important thing is that the returned value should be assigned a value of long at the beginning because it may cross the border)
Then there are some special situations for analysis.
Class Solution {public: int atoi (string str) {if (str. length () = 0) {return 0;} long retVal = 0; int length = str. length (); int I = 0; char signFlag = 1; while (str [I] = '') {I ++ ;} if (str [I] = '-') {I ++; signFlag =-1;} else if (str [I] = '+ ') {I ++ ;}for (; I <length; I ++) {int n = str [I]-'0'; if (n <0) | (n> 9) {return retVal * signFlag; // <special symbol displayed} retVal = retVal * 10 + n; if (retVal * signFlag> INT_MAX) {return INT_MAX;} else if (retVal * signFlag <INT_MIN) {return INT_MIN ;}} return retVal * signFlag ;}};