LeetCode算法題——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.

算法思想:對字符串進行分類判斷,以及對轉換後溢出問題進行處理
C++ Code:
#include <iostream>
#include "stdlib.h"
#include "string.h"
using namespace std;

class Solution {
public:
    int myAtoi(string str) {
        int maxNum=2147483647;
        int minNum=-2147483648;
        char *maxNumber="2147483647";
        bool overFlowFlag=false;
        char *minNumber="2147483648";
        bool isNeg=false;
        int i=0,sum=0,j=0;
        while(str[i]==' '){
            i++;
        }
        if(str[i]=='-'){
            isNeg=true;
            i++;
        }else if(str[i]=='+'){
            i++;
        }else if(str[i]<'0'||str[i]>'9'){
            return 0;
        }
        int iTemp=i;
        while(i<str.length()&&str[i]>='0'&&str[i]<='9'){
            if(j<9){
                sum=sum*10+str[i]-'0';
                j++;
                i++;
            }else if(j==9){
                char ap[10];
                for(int s=0;s<=j;s++){
                    ap[s]=str[iTemp+s];
                }
                int res;
                if(isNeg){
                    res=strcmp(minNumber,ap);
                }else{
                    res=strcmp(maxNumber,ap);
                }
                if(res>0){
                    sum=sum*10+str[i]-'0';
                    j++;
                    i++;
                }else{
                    if(isNeg){
                        return minNum;
                    }else{
                        return maxNum;
                    }   
                }
            }else{
                if(isNeg){
                    return minNum;
                }else{
                    return maxNum;
                }
            }

        }
        if(isNeg){
            return -sum;
        }
        return sum;
    }
};

int main(){
    Solution sol;
    int res=sol.myAtoi("  -2147483as65925252");
    cout<<res<<endl;
    char buffer[10]="32j5lk";
    int s=atoi(buffer);
    cout<<s<<endl;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章