leetcode:String to Integer (atoi)

Question:

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.

实现atoi这个函数, public int atoi(String str),传入字符串str可以返回整数,请仔细考虑一下字符串的各种情况,如果你想挑战一下就不要看下边的注意事项。这是一个比较简单的leetcode题。

注意事项:① 这个功能开始要丢弃尽量多的空格直到第一个非空格字符的出现(其实就是去除字符串开头空格),把第一个非空格字符“+”或者“-”当做数字符号处理。

     ② 这个字符串可以在数字后加入字母,要忽略处理。

     ③ 如果第一个非空格字符是字母而不是一个数字符号,或者根本就是空,就不能使用转换功能,返回0。

     ④ 如果正确的结果已经超出了整型数的范围返回INT_MAX (2147483647) 或者INT_MIN (-2147483648) 。

算法设计思想:① 首先,去掉字符串前边空格,并判断字符串是否为空,为空直接就返回0,这里要注意一点判断字符串是否为“”要用equals方法而不是使用==。

                         ② 去除字符串前边空格后,提取第一个字符,判断是否是‘+’或者‘-’,用boolean类型bool做标记,如果正数就是true,负数就是false,设计这个的目的是想让处理的数是绝对值,最后再判断符号。

                         ③ 去除字符串前边的空格,如果第一个字符不是数字,返回0。

                         ④ 对字符串访问,用start和end标记数字,如果遇到非数字,访问结束,用start和end截取数字字符。

                         ⑤ 对截取数字字符做类型转化,判断是否超出了范围,如果超出了范围,返回INT_MAX (2147483647) 或者INT_MIN (-2147483648)。

代码实现(java)

 1 class Solution8 {
 2     public int atoi(String str) {
 3         //str=str.replace(" ", "");//去掉字符串中所有的空格
 4         str=str.trim();//去掉两边空格
 5         if(str.equals("")||str==null)//如果字符串是空或者第一个是字母,则返回0
 6             return 0;
 7         int i=0;            //对字符串遍历,指向字符串的下标
 8         boolean bool=true;  //记录正负数
 9         if(str.charAt(i)=='-') {//判断第一个字符是不是正负号
10             bool=false;
11             i++;
12         }
13         else if(str.charAt(i)=='+'){
14             i++;
15         }
16         if(str.charAt(i)<'0'||str.charAt(i)>'9')//如果非空格字符不是数字返回0
17             return 0;
18         int start=i;//数字开始的下标
19         int end=i;//数字结束的位置
20         for(;i<str.length();){
21             if(str.charAt(i)>='0'&&str.charAt(i)<='9')
22                 i++;
23             else{
24                 end=i;
25                 break;
26             }
27         }
28         if(i==str.length())//如果i==str.length,表明end的值就是字符串的最后一位,但是这时end并不是str.length而是str。length-1
29             end=i;
30         if(Double.valueOf(str.substring(start, end))>Integer.MAX_VALUE){
31             if(bool)
32                 return Integer.MAX_VALUE;  //如果超出范围且为正数返回最大Integer数
33             else
34                 return Integer.MIN_VALUE;//如果超出范围且为负数返回最小Integer数
35         }
36         else{
37             if(bool)
38                 return Integer.valueOf(str.substring(start, end));//返回正数
39             else
40                 return -Integer.valueOf(str.substring(start, end));//返回负数
41         }
42     }
43 }

 

 

      

原文地址:https://www.cnblogs.com/zhaolizhen/p/atoi.html