您的位置:首页 > 其它

8. String to Integer (atoi)

2016-07-15 11:31 274 查看
Implement atoi to convert a string to an integer.

Hint: Carefully consider all possible input cases. If you want a challenge, please do not see below and ask yourself what are the possible input cases.

Notes: It is intended for this problem to be specified vaguely (ie, no given input specs). You are responsible to gather all the input requirements up front.

Update (2015-02-10):

The signature of the 
C++
 function had been updated. If you still see your
function signature accepts a 
const char *
 argument, please click the reload
button  to reset your code definition.

spoilers alert... click to show requirements for atoi.

Requirements for atoi:
The function first discards as many whitespace characters as necessary until the first non-whitespace character is found. Then, starting from this character, takes an optional initial plus or minus sign followed
by as many numerical digits as possible, and interprets them as a numerical value.
The string can contain additional characters after those that form the integral number, which are ignored and have no effect on the behavior of this function.
If the first sequence of non-whitespace characters in str is not a valid integral number, or if no such sequence exists because either str is empty or it contains only whitespace characters, no conversion is performed.
If no valid conversion could be performed, a zero value is returned. If the correct value is out of the range of representable values, INT_MAX (2147483647) or INT_MIN (-2147483648) is returned.

自己的好丑好混乱
public static int myAtoi(String str)
{
int len=str.length();

if(len==0)
return 0;

char[] carr=str.toCharArray();

int templen=0;
char[] temp=new char[len];

int start=0;
while(carr[start]==' ')
start++;

for(int i=start;i<len;i++)
temp[templen++]=carr[i];

carr=temp;
len=templen;

boolean isnegative=carr[0]=='-'?true:false;

if(carr[0]=='+'||carr[0]=='-')
{
char[] t=new char[len-1];
System.arraycopy(carr, 1, t, 0, len-1);
carr=t;
len--;
}

int illegalposition=-1;

for(int k=0;k<len;k++)
if(carr[k]<'0'||carr[k]>'9')
{
illegalposition=k;
break;
}

if(illegalposition==0)
return 0;

if(illegalposition>0)
len=illegalposition;

if(isnegative&&len>10)
return -2147483648;
if(len>10)
return 2147483647;

if(len==10)
{
char[] pomaxrange={'2','1','4','7','4','8','3','6','4','7'};
char[] nemaxrange={'2','1','4','7','4','8','3','6','4','8'};
boolean inrange=true;
if(isnegative)
{
int base=1000000000;
int sum=0;
for(int i=0;i<len;i++)
{
sum+=(carr[i]-nemaxrange[i])*base;
base/=10;
}
if(carr[0]-nemaxrange[0]>1||sum>0)
inrange=false;
}
else {
int base=1000000000;
int sum=0;
for(int i=0;i<len;i++)
{
sum+=(carr[i]-pomaxrange[i])*base;
base/=10;
}
if(carr[0]-pomaxrange[0]>1||sum>0)
inrange=false;
}

if(!inrange)
if(isnegative)
return -2147483648;
else {
return 2147483647;
}

}

int base=1;
for(int i=0;i<len-1;i++)
base*=10;

int result=0;
for(int i=0;i<len;i++)
{
result+=(carr[i]-'0')*base;
base/=10;
}

result=isnegative?-result:result;

return result;
}

--------------------------------------------------------------------------------------------
还是借鉴别人简洁明了的吧。摘自https://discuss.leetcode.com/topic/2666/my-simple-solution/7

 we only need to handle four cases:
discards all leading whitespaces
sign of the number
overflow
invalid input

public static int myAtoi(String str) {
if (str.isEmpty()) return 0;
int sign = 1, base = 0, i = 0;
while (str.charAt(i) == ' ')
i++;
if (str.charAt(i) == '-' || str.charAt(i) == '+')
sign = str.charAt(i++) == '-' ? -1 : 1;
while (i < str.length() && str.charAt(i) >= '0' && str.charAt(i) <= '9') {
if (base > Integer.MAX_VALUE / 10 || (base == Integer.MAX_VALUE / 10 && str.charAt(i) - '0' > 7)) {
return (sign == 1) ? Integer.MAX_VALUE : Integer.MIN_VALUE;
}
base = 10 * base + (str.charAt(i++) - '0');
}
return base * sign;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: