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.
解题思路:本题是将字符串转换成一个整型数,前提需要判断字符串的合法性。
先判断字符串是否为空,为空直接返回为0;接着使用字符串中的trim()方法,去掉字符串str头尾的空格得到新的字符串newStrTrim;接着,判断新字符串的长度:
(1).length=1,若字符的非数字,直接返回0,否则直接输出该数字;
(2).length>1,又需要分情况处理。第一种情况:新字符串的第一个字符非数字,非'+',非'-',则输出为0。 第二种情况:新字符串第一个字符为'+'和'-',第二个字符非数字,则输出为0。第三种情况处理分2中形式分析:第一种形式:第一个字符为数字循环遍历,如为数字添加到StringBuilder保存,否则退出循环。第二种形式:第一个字符为'+'或'-',若为'-',将该字符添加到StringBuilder中,循环遍历下标从1开始遍历,因为'+'不需要添加到StringBuilder中。然后判断StringBuilder中第一个元素是否为'-',若是,则处理负数边界处理,若不是,则处理整数边界处理。
暂时还没想出其他的方法来解决该题,以便缩短代码,更简便的方法还是实现中。下面的代码已经在LeetCode上面通过。
public static int myAtoi(String str) {
StringBuilder sb = new StringBuilder();
//先判断字符串是否为空
if(str == "") {
return 0;
}
//去掉头尾的空格的新字符串
String newStrTrim = str.trim();
//判断新字符串的长度为1
if(newStrTrim.length() == 1) {
//若字符非数字,直接输出为0,否则直接输出并转换该字符串
if(!Character.isDigit(newStrTrim.charAt(0))) {
return 0;
}else {
return Integer.parseInt(newStrTrim);
}
}else if(newStrTrim.length() > 1){//新字符串长度大于1
char ch0 = newStrTrim.charAt(0);
char ch1 = newStrTrim.charAt(1);
if(!Character.isDigit(ch0)&&ch0!='+'&&ch0!='-') {
//第一种情况:新字符串的第一个字符非数字,非'+',非'-',则输出为0.
return 0;
}else if((ch0=='+'||ch0=='-')&&!Character.isDigit(ch1)){
//第二种情况:新字符串第一个字符为'+'和'-',第二个字符非数字,则输出为0.
return 0;
}else {
//第三种情况处理分2中形式分析:
// 第一种形式:第一个字符为数字循环遍历,如为数字添加到StringBuilder保存,否则退出循环。
// 第二种形式:第一个字符为'+'或'-',若为'-',将该字符添加到StringBuilder中,循环遍历下标
// 从1开始遍历,因为'+'不需要添加到StringBuilder中。
if(Character.isDigit(ch0)) {
for (int i = 0; i < newStrTrim.length(); i++) {
if(!Character.isDigit(newStrTrim.charAt(i))) {
break;
}
sb.append(newStrTrim.charAt(i));
}
}else {
if(ch0 == '-'){
sb.append(ch0);
}
for (int i = 1; i < newStrTrim.length(); i++) {
if(!Character.isDigit(newStrTrim.charAt(i))) {
break;
}
sb.append(newStrTrim.charAt(i));
}
}
if(sb.charAt(0) == '-') {
if(sb.toString().length() > 11 || Long.parseLong(sb.toString()) < -2147483647) {
return -2147483648;
}else {
return Integer.parseInt(sb.toString());
}
}else {
if(sb.toString().length() > 10 || Long.parseLong(sb.toString()) > 2147483647 ) {
return 2147483647;
}else {
return Integer.parseInt(sb.toString());
}
}
}
}else {
return 0;
}
}