leetcode之atoi

发布时间:2014-10-22 13:08:45
来源:分享查询网

class Solution { public:     int atoi(const char *str) {         // Start typing your C/C++ solution below         // DO NOT write int main() function         if (str == NULL) {             return 0;         }         bool isNegtive = false;         char* start = const_cast<char*>( str);         int len = strlen(str);         char* end = start+len-1;         if (*start == '-') {             isNegtive = true;             start++;         }         if (*start=='+') {             isNegtive = false;             start++;         }         int res = 0;         bool first = true;         while (start <= end) {             if (first&&*start=='0') {                 start++;             }             if (isdigit(*start)) {                 first = false;                 res = res*10+(*start-'0');                             }              start++;         }         if (isNegtive) {             res = 0-res;         }         return res;     } };

返回顶部
查看电脑版