码迷,mamicode.com
首页 > 其他好文 > 详细

String to Integer (atoi) leetcode

时间:2015-01-13 23:22:36      阅读:428      评论:0      收藏:0      [点我收藏+]

标签:leetcode   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、前面的空格符号     如  “_ _ _ 123”  前面的下划线表示空格    处理方法(忽略)

2、空格后面可能有正负号   正号我们不需要太关心 ,但是当正负号同时出现就错了   ,负号需要重点记录,因为后面还有溢出情况

3、正负号后面的 0    如    “+0000123”   处理方法(忽略)

4、其他字符  如果出现其他字符返回前面已经转换好的数字    如   “+000012abc45”  返回  12 ,如  “+abc123” 返回result=0   result初始值为0

5、溢出情况  需要保留负号是否出现情况


代码如下:

<span style="font-family:Arial;font-size:18px;">class Solution {
public:
    int atoi(const char *str) {
        
        if(str==NULL)
            return 0;
            
            
        while(*str==' ')  //表示忽略字符串前面出现的空格
            ++str;
            
        bool positive =false ;    
        if(*str=='+')    //表示前面是否有正号
        {
            positive=true;
            ++str;
        }
        bool negative = false;//表示是否为负数,前面是否有负号
        if(*str=='-')
        {
            negative=true;
            ++str;
        }
        
        while(*str=='0')//消除正负号后面的 0 
            ++str;
        
        
        if(negative&&positive) //表示正负号同时有  错误
            return 0;
            
        long long result=0;
        
        while(*str!='\0')
        {
           
            if(*str>='0'&&*str<='9')
            {
                result=result*10+*str-'0';
                
                if(negative==false&&result>INT_MAX) // 判断该数是不是大于最大数
                      return INT_MAX;
                      
                if(negative==true&&-result<INT_MIN)  //判断该数是不是小于最小数
                    return INT_MIN; 
            }
            else
            {
                break;
            }
            ++str;
        }
        if(negative==true)  //表示该数为负数 
            return -result;
        if(negative==false) //表示该数为正数
            return result;
    }
};</span>




String to Integer (atoi) leetcode

标签:leetcode   atoi   

原文地址:http://blog.csdn.net/yujin753/article/details/42684811

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!