首页 > 代码库 > String to Integer (atoi)

String to Integer (atoi)

这道题知道了用long long 这种变量类型,从最初的128ms优化到68ms

代码如下:

class Solution {public:    int atoi(const char *str) {        if(str == NULL)		return 0;		int i=0;		int flag =1;			string tempstr;		while(str[i] != ‘\0‘)		{           if(str[i] == ‘ ‘)		   	 i++;		   else		   	 break;		}			if(str[i] == ‘-‘)		{           flag =-1;		   i++;		}		else if(str[i] == ‘+‘)		{		   i++;		}		while(str[i] != ‘\0‘)		{           if(str[i] == ‘0‘)		   	 i++;		   else		   	 break;		}  		long long data = http://www.mamicode.com/0;>

  

String to Integer (atoi)