[leetcode] #8 String to Integer (atoi)

来源:互联网 发布:windows hadoop2 安装 编辑:程序博客网 时间:2024/05/18 03:38

[leetcode] #8 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.

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、长度大于10,如果是负数赋值为-2147483648,正数赋值为2147483647.
2、长度等于10,将字符串中每个字符与字符串“2147483648”进行相应地比较,越界根据第1点赋值,不越界按照第三点赋值。
3、长度小于10,正常转化数字

#include <string>using namespace std;#include <cstdio>//       +004500//       -0012a42class Solution {public:    int myAtoi(string str) {        unsigned int len = str.length();        int num = 0;        int spacnt = 0, fuhaocnt = 0; // 记录空格个数,符号个数         char s[12] = "2147483647";//int 正整数最大值        int flag = 0;//判断正数或者负数        //记录空格个数        for (int j = 0; j < len; j++) {            if (str[j] != ' ') {                break;            } else {                spacnt++;            }        }        //记录符号个数        for (int j = spacnt; j < len; j++) {            if (str[j] == '+') {                fuhaocnt++;            } else if (str[j] == '-') {                flag++;                fuhaocnt++;                   } else {                break;            }        }        //记录数字字符串末位置        int j;        for (j = spacnt+fuhaocnt;j < len;j++) {            if (str[j] < '0' || str[j] > '9') {                break;            }        }        len = j;        int cnt = 0;        int yuejie = 0;//判断越界        //判断数字字符串长度是否大于10,越界        if (len-spacnt-fuhaocnt > 10) {            yuejie = 1;        }        //判断数字字符串长度为10,跟int最大正整数判断是否越界        if (len-spacnt-fuhaocnt == 10) {            for (int i = spacnt+fuhaocnt; i < spacnt+fuhaocnt+10; i++) {                if (str[i] > s[cnt]) {                    yuejie = 1;                    break;                } else if(str[i] < s[cnt]) {                    break;                }                cnt++;            }        }        //正数越界        if (yuejie && !flag)            num = 2147483647;        //负数越界        if (yuejie && flag)            num = -2147483648;        //不越界,字符串转化为数字        for (int i = spacnt+fuhaocnt; i < len && !yuejie; i++) {            num = num * 10 + (str[i] - '0');        }        //不越界,变成负数        if (flag && !yuejie) {            num*=-1;        }        //前面出现多余符号,赋值为0        if (fuhaocnt >= 2)            num = 0;        return num;    }};
0 0