LeetCode-StringtoInteger(atoi)

王朝学院·作者佚名  2016-08-27
窄屏简体版  字體: 小  |  中  |  大  |  超大  

题目:

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 thisPRoblem to be specified vaguely (ie, no given input specs). You are responsible to gather all the input requirements up front.

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.

思路:

基本运算;注意判断乘和加的溢出。

packagemanipulation;publicclassStringToInteger {publicintmyAtoi(String str) {intlen;if(str ==null|| (len = str.length()) == 0)return0;intpos = 0;intsign = 1;intres = 0;while(Character.isWhitespace(str.charAt(pos))) ++pos;if(str.charAt(pos) == '+') {++pos;

}elseif(str.charAt(pos) == '-') {++pos;

sign= -1;

}while(pos < len &&Character.isDigit(str.charAt(pos))){intvalue = str.charAt(pos) - '0';intpreRes =res;

// We use Math.abs here because on the second time, res will become positive if sign == -1

res= Math.abs(res) * 10 *sign;if(res/(10 * sign) !=Math.abs(preRes)) {returnsign > 0 ?Integer.MAX_VALUE : Integer.MIN_VALUE;

}

preRes=res;

res= res + sign *value;if(sign == 1 && res <preRes)returnInteger.MAX_VALUE;if(sign == -1 && res >preRes)returnInteger.MIN_VALUE;++pos;

}returnres;

}publicstaticvoidmain(String[] args) {//TODO Auto-generated method stubString str = "-123";

StringToInteger sti=newStringToInteger();

System.out.println(sti.myAtoi(str));

}

}

代码更简洁一些:

packagemanipulation;publicclassStringToInteger {publicintmyAtoi(String str) {intlen;if(str ==null|| (len = str.length()) == 0)return0;intpos = 0;intsign = 1;intres = 0;while(Character.isWhitespace(str.charAt(pos))) ++pos;if(str.charAt(pos) == '+') {++pos;

}elseif(str.charAt(pos) == '-') {++pos;

sign= -1;

}while(pos < len &&Character.isDigit(str.charAt(pos))) {intvalue = str.charAt(pos) - '0';if(sign == 1 && res > (Integer.MAX_VALUE - value) / 10)returnInteger.MAX_VALUE;if(sign == -1 && (-res < (Integer.MIN_VALUE + value) / 10))returnInteger.MIN_VALUE;

res= res * 10 +value;++pos;

}returnsign > 0 ? res : -res;

}publicstaticvoidmain(String[] args) {//TODO Auto-generated method stubString str = "-2147483648";

StringToInteger sti=newStringToInteger();

System.out.println(sti.myAtoi(str));//-(INT_MIN_VALUE) is still INT_MIN_VALUEintx = -2147483648;

System.out.println(-x);

}

}

 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
© 2005- 王朝網路 版權所有 導航