/*
** 将字符串转换为数字——LeetCode 第 8 题
** author: aliao
*/
#include <stdio.h>
#include <limits.h>
int myAtoi(char *str)
{
int i = 0;
int start = 0;
int len = 0;
// 遍历字符串中起始的空格字符
while (str[i] == 32)
{i++;}
// 如果非空首字符不是 -+ 或者 0 - 9 这三类字符,则间接返回 0
// 如果是空字符串或者字符串只蕴含空白子字符
if (str[i] != '-' && str[i] != '+' && (str[i] < 48 || str[i] > 57))
{return 0;}
else
{
start = i;
len += 1;
i++;
}
while (str[i] != '\0')
{if (str[i] >= 48 && str[i] <= 57)
{
len += 1;
i++;
}
else
{break;}
}
int reverse = 0;
int k = 10;
int isNeg = 0;
for (int j = 0; j < len; j++)
{if (j == 0)
{if (str[start + j] == '-')
{
isNeg = 1;
continue;
}
else if (str[start + j] == '+')
{continue;}
}
if (isNeg)
{if (reverse < INT_MIN / 10 || reverse == INT_MIN / 10 && str[start + j] - 48 > 8)
{return INT_MIN;}
else
{reverse = reverse * k - (str[start + j] - 48);
}
}
else
{if (reverse > INT_MAX / 10 || reverse == INT_MAX / 10 && str[start + j] - 48 > 7)
{return INT_MAX;}
else
{reverse = reverse * k + (str[start + j] - 48);
}
}
}
return reverse;
}
int main (void) {
char *str = "-2147483648";
printf("final value: %d", myAtoi(str));
}
执行用时:0 ms, 在所有 C 提交中击败了 100.00% 的用户
内存耗费:5.8 MB, 在所有 C 提交中击败了 6.09% 的用户
工夫复杂度 O(n),空间复杂度 O(1)