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.
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.
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.
A:
一开始,竟然没敢把题目看完,就不敢做了。哎,真是可笑,竟然留到了最后才做,这么一道简单的题目。 ╮(╯▽╰)╭ 可悲啊, Tiger
思路很简单,就是一个个地数呗。
public class Solution { public int atoi(String str) { // Note: The Solution object is instantiated only once and is reused by // each test case. int INT_MAX = 2147483647; int INT_MIN = -2147483648; StringBuffer buf = new StringBuffer(str.trim());// ignore space at two // ends int sign = 1; if (buf.length() == 0) return 0; if (buf.charAt(0) == '+') { // delete + buf.delete(0,1); } else if (buf.charAt(0) == '-') { sign = -1; buf.delete(0, 1); } str = buf.toString(); long sum = 0; // adding from back to begin for (int i = 0; i < str.length(); i++) { char ch = str.charAt(i); if (ch < '0' || ch > '9') break; int val = ch - '0'; sum = sum * 10 + val; if (sign == 1) { if (sum > INT_MAX) { return INT_MAX; } } else { // sum should be negative if (sign * sum < INT_MIN) { return INT_MIN; } } } return (int) (sign * sum); } }
Mistakes:
1: 题意理解错误, : 当超出范围时, 返回最大或最小的。 而不是返回 0.
2; 自己宽泛了题意。 当 + -符合后面,是不能再有空格的。
1: 要注意:
return (int) (sign * sum); 这里, 由于sum 是long 型的, 如果仅仅写成
return (int) sign * sum; 是不对的, 会先只转变 sign 的类型。
--------------第二遍--------------
1: 刚开始,直接开始 考虑str[0] 的值。 而没有先考虑,是否存在 0 位置。
*********************每次计算之前判断是否越界****************
public class Solution { public int myAtoi(String str) { boolean isNeg = false; str = str.trim(); if(str.length()==0) return 0; if(str.charAt(0)=='+' || str.charAt(0)=='-'){ isNeg = str.charAt(0)=='-'; str = str.substring(1); } int sum = 0; for(int i =0;i<str.length();i++){ char ch = str.charAt(i); if(Character.isDigit(ch)==false) break; if(isNeg && (sum < Integer.MIN_VALUE/10 || (sum == Integer.MIN_VALUE/10 && ch>= '8') )) return Integer.MIN_VALUE; if( !isNeg && (-sum > Integer.MAX_VALUE/10 || ( -sum == Integer.MAX_VALUE/10 && ch>='7'))) return Integer.MAX_VALUE; sum = sum * 10 - (ch -'0'); } return isNeg?sum:-sum; } }
这里,或者直接用long 来记录结果,然后对比即可。
No comments:
Post a Comment