【Leetcode Algorithm】String to Integer (atoi)

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.

几种需要考虑的情况:
1.数字前面有空格 如s=“    123”返回123
2.数字前出现了不必要或多于的字符导致数字认证错误,返回0   如s=“   b1234”  , s=“ +-1121”
3.数字中出现了不必要的字符,返回字符前的数字 如s=“   12a12” 返回12
4.溢出情况,超过了范围(-2147483648~2147483647) 若超过了负数的 输出-2147483648  超过了正数的输出2147483647
在科普一个知识点,倘若某个数超过了2147483647则会变为负数,反过来一样

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
public  class  Solution {
     public  int  myAtoi(String str) {
         //使用正则表达式去掉字符串中空格字符
         str = str.trim();
         //flag为字符串所包含的符号,-1代表负数,1代表正数
         int  flag =  1 ;
         //用来判断是否有+-或者-+出现
         int  preFlagNum =  0 ;
         int  curFlagNum =  0 ;
         //result代表字符串中的整型数
         double  result =  0 ;
         //表示除去符号位剩下的数字位的位置
         int  begin =  0 ;
         //如果str为空则无整数存在,返回0
         if (str.length()> 0 ){
             //判断正负数
             if (!Character.isDigit(str.charAt( 0 ))){
                 for ( int  i= 0 ;i<str.length();i++){
                     if (str.charAt(i)== '-' ){
                         flag = -flag;
                         //判断是否有-+的情况出现,若有,则返回0
                         preFlagNum = curFlagNum;
                         curFlagNum--;
                         if (i!= 0 &&curFlagNum<= 0 &&curFlagNum<preFlagNum){
                             return  0 ;
                         }
                     }
                     else   if (str.charAt(i)== '+' ){
                         preFlagNum = curFlagNum;
                         curFlagNum++;
                         if (i!= 0 &&curFlagNum>= 0 &&curFlagNum>preFlagNum){
                             return  0 ;
                         }
                     }
                     else {
                         break ;
                     }
                     begin++;
                 }
             }
             //计算数字部分的值
             while (str.length()>begin&&str.charAt(begin)>= '0' &&str.charAt(begin)<= '9' ){
                 result = result *  10  + (str.charAt(begin) -  '0' );
                 begin++;
             }
             //负数加负号
             if (flag==- 1 ){
                 result = -result;
             }
             //判断是否超出最大整数值
             if  (result > Integer.MAX_VALUE)
                 return  Integer.MAX_VALUE;
             //判断是否超出最小整数值
             if  (result < Integer.MIN_VALUE)
                 return  Integer.MIN_VALUE;
             //转换为整型
             return  ( int ) result;
         }
         else {
             return  0 ;
         }
     }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值