String to Integer (atoi)(算法分析week6)

来源:互联网 发布:东北师大网络教育2018 编辑:程序博客网 时间:2024/06/06 14:01

String to Integer (atoi)

题目来源:https://leetcode.com/problems/algorithm/

-问题描述-
-解体思路-
-代码实现-

问题描述

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.

解题思路

这道题并不用考虑算法的性能,时间复杂度,空间复杂度等。这道题主要是要考虑到所有的输入情况。总结需要下面几个步骤对字符串进行处理解决问题。
(1)判断字符串长度是否为0,如果是,返回0
(2)找到字符串中第i位字符,第i位字符为字符串中第一个非空格字符。如果字符串中全部为空格字符,返回0;
(3)判断第i位字符是否为’+’或’-‘或在’1’到’9’之间的数字。如果第i位不满足以上条件,则返回0;
(4)设置变量flag表示符号位,并初始化为1。第i位字符若为’-‘,符号位flag = -1, i++;第i位字符若为’+’,i++;
(5)变量number记录数字的大小,初始化为0;
(6)从第i位字符开始,判断该字符是否为’0’到’9’之间的数字,如果是,number = number*10 + s[i] - ‘0’,判断number*flag与INT_MAX、INT_MIN大小,如果number*flag > INT_MAX,返回INT_MAX,如果number*flag < INT_MIN,返回INT_MIN;如果该字符不是’0’到’9’的数字,终止循环。
(7)number = number*flag;如果number > INT_MAX,返回INT_MAX,如果number < INT_MIN,返回INT_MIN,否则返回number.

代码实现

@requires_authorizationclass Solution {public:    int myAtoi(string str) {        int i;        for (i = 0; i < str.length(); i++) {            if (str[i] != ' ') break;         }        int flag = 1;        if (str.length() == 0 || i == str.length()) return 0;        if (str[i] == '+' || str[i] == '-') {            if (!(str[i + 1] >= '0' && str[i + 1] <= '9')) return 0;         }        if (str[i] == '+' || str[i] == '-' &&(str[i+1] >= '0' || str[i+1] <= '9')) {            if (str[i] == '+') {                flag = 1;                i++;            }            else if (str[i] == '-') {                flag = -1;                i++;            }        }        if (str[i] != '+' && str[i] != '-' && !(str[i] >= '0' && str[i] <= '9')) {            return 0;        }      long long int number = 0;        int t = i;        for (i; i < str.length(); i++) {            if (str[i] >= '0' && str[i] <= '9') {                number = number*10 + str[i] - '0';                if (number*flag > INT_MAX)return INT_MAX;                if (number*flag < INT_MIN) return INT_MIN;            }            if (!(str[i] >= '0' && str[i] <= '9')) break;        }        number = number*flag;        return number;    }};
原创粉丝点击