@@ -0,0 +1,58 @@
|
||||
// This file is generated by mkfile.py
|
||||
// Date: 2026-06-25
|
||||
|
||||
#include <solution/8.h>
|
||||
|
||||
#include <limits.h>
|
||||
#include <stdbool.h>
|
||||
int myAtoi(char *s)
|
||||
{
|
||||
long ret = 0;
|
||||
int length = 0;
|
||||
bool isPos = true;
|
||||
bool hasSign = false;
|
||||
bool hasDigit = false;
|
||||
bool hasSpace = true;
|
||||
|
||||
for (; *s != '\0' && length < 11; s++)
|
||||
{
|
||||
// step 1 skip space
|
||||
if (hasSpace)
|
||||
{
|
||||
if (*s == ' ')
|
||||
continue;
|
||||
hasSpace = false;
|
||||
}
|
||||
// step 2 check is sign while has no number
|
||||
if (!hasDigit && !hasSign && (*s == '-' || *s == '+'))
|
||||
{
|
||||
hasSign = true;
|
||||
isPos = *s == '+';
|
||||
continue;
|
||||
}
|
||||
// step 3 read num
|
||||
if (*s - '0' >= 0 && *s - '9' <= 0)
|
||||
{
|
||||
hasDigit = true;
|
||||
// check if front zero
|
||||
if (length == 0 && *s == '0')
|
||||
continue;
|
||||
|
||||
// not front zero
|
||||
if (length > 0)
|
||||
ret *= 10;
|
||||
ret += *s - '0';
|
||||
length++;
|
||||
continue;
|
||||
}
|
||||
// step 4 check char
|
||||
break;
|
||||
}
|
||||
|
||||
if (isPos)
|
||||
ret = ret > INT_MAX ? INT_MAX : ret;
|
||||
else
|
||||
ret = ret * -1 < INT_MIN ? INT_MIN : ret * -1;
|
||||
|
||||
return (int)ret;
|
||||
}
|
||||
Reference in New Issue
Block a user