【leetcode】8. String to integer (atoi)

atoi 问题即ASCII to integer 问题,这个题目要考虑的问题很多,同时也是数论类算法经常考虑的问题,这里我们先对它们进行一个简单的总结。

数论类算法要考虑的问题:

1, 0 , 负数,小数

2, 是否考虑错误输入,就像本题可能会有一些空格的考虑或者是其它字符的考虑

3,溢出的问题,即是否可能会产生溢出,int 是 4 个字节,范围为  [ - 2 ^31, 2^31-1] 可用16进制数 0x80000000(10000...)与0x7fffffff(0111...)分别表示,是有符号数,转化为二进制后,第一位0或者1表示符号,long一般都认为也是4个字节,而long long 是8个字节,在用int产生溢出时,用long long接收结果则不会产生溢出,c++ 中用 INT_MIN 和 INT_MAX 来直接表示int的取值上下限。注意上下限范围差1,经常需要仔细考虑一下问题。

 

关于INT_MIN 和 INT_MAX 的一些小实验:

#include <iostream>
using namespace std;

int main()
{
	cout <<"INT_MAX: "<< INT_MAX << endl;
	cout <<"INT_MIN: "<< INT_MIN << endl;
	cout <<"INT_MIN - 1: "<< INT_MIN - 1 << endl;
	cout << "INT_MAX + 1: " << INT_MAX + 1 << endl;
	cout <<"INT_MIN * (-1): "<< -INT_MIN  << endl;
	cout << "INT_MAX * (-1): " << -INT_MAX << endl;
	cout <<"INT_MIN + INT_MAX: "<< INT_MIN + INT_MAX << endl;
	long long m = 9223372036854775808;
	if (m > INT_MAX)
		cout << "m > INT_MAX" << endl;
	else if( m < INT_MAX)
		cout << "m ( 9223372036854775808) < INT_MAX" << endl;
	return 0;
}

 结果:

按照正常情况long long 型的与 int 型的进行比较,应当是int进行强制类型转换到 long  long型的,但比较的结果却是大的数小于INT_MAX,实际上理解错了,9223372036854775808 ,实际上,INT_FAST64_MAX :9223372036854775807, 即8个字节的 INT_MAX要小于所给的这个数,这个数不能用8个字节表示,所以正常的long long 与 int 以及 long long之间完全可以正常的按照大小比较,int型也确实是强制转换为long long 型的,如下面的这个程序:

#include <iostream>
#include<math.h>
using namespace std;

int main()
{
	cout << "INT_FAST64_MAX: "<< INT_FAST64_MAX << endl;
	long long m = pow(2, 34);
	long long n = pow(2, 36);
	if (m < n)
		cout << "long long : 2^34 < 2^36" << endl;
	int i = pow(2, 23);
	if (n > i)
		cout << "int vs long long : 2^23 < 2^36" << endl;
	return 0;
}

结果截图:

 

故所给的这个超出64位能够表示的例子是下面解决办法中 if(res > max) res = max的最主要的原因甚至唯一的原因,其它的大于INT_MAX因为res是long long 型的,所以都可以得到解决,但是为了解决这个超出64位的数,需要我们特别地写出这句话进行判断。 

现在是题目的介绍:

Implement atoi which converts a string to an integer.

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.

Note:

  • Only the space character ' ' is considered as whitespace character.
  • Assume we are dealing with an environment which could only store integers within the 32-bit signed integer range: [−231,  231 − 1]. If the numerical value is out of the range of representable values, INT_MAX (231 − 1) or INT_MIN (−231) is returned.

Example 1:

Input: "42"
Output: 42

Example 2:

Input: "   -42"
Output: -42
Explanation: The first non-whitespace character is '-', which is the minus sign.
             Then take as many numerical digits as possible, which gets 42.

Example 3:

Input: "4193 with words"
Output: 4193
Explanation: Conversion stops at digit '3' as the next character is not a numerical digit.

Example 4:

Input: "words and 987"
Output: 0
Explanation: The first non-whitespace character is 'w', which is not a numerical 
             digit or a +/- sign. Therefore no valid conversion could be performed.

Example 5:

Input: "-91283472332"
Output: -2147483648
Explanation: The number "-91283472332" is out of the range of a 32-bit signed integer.
             Thefore INT_MIN (−231) is returned.

 题目大概就是给出一个字符串将它转化为整数,这个字符串必须是有效的如 “  +1113  ”, 前后可以有空格,也可以有+ - 号,但不能有2个正负号,也不能有其它的字符如"  1x333" 就是错误的,错误的要输出 0,当然所给的例子其实如上面一样没有这种嵌入的,都是1串数字前后可能跟着其它字符,将字符放在前面的就是错误的,放在后面的不读,直接break只对读到的数字进行处理。

代码:

class Solution {
public:
    int myAtoi(string str) {
        int i = 0;
        long long int res = 0;
        bool flag = false;
        const int minInt = 0x80000000;
        const int maxInt = 0x7fffffff;
        //long long 8个字节可以解决int溢出的问题
        const long long max = 0x100000000;
        //用双引号和单引号都可以表示空格符,无差,最好用单引号表示单个字符
        while(i < str.length() && str[i] == ' ')
            i++;
        if(str[i] == '+')
            i++;
        //因为例子中有一个+-2要求输出为0,所以要用if-else对一个+-进行判断
        //两个+-号是错误的
        else if(str[i] == '-')
        {
            i++;
            flag = true;
        }
        for(; i < str.length(); i++)
        {
            if(str[i] >= '0' && str[i] <= '9')
            {
                res = res * 10 + str[i] - '0';
                if(res > max)
                {
                    res = max;                   
                }               
            }
            //不能有不是数字的字符
            else
                break;
        }
        if(flag == true)
            res = -res;
        if(res < minInt)
            res = minInt;
        if(res > maxInt)
            res = maxInt;
        return res;
        
    }
};

当然,直接用 INT_MIN 和 INT_MAX来直接解决即可,不用自己定义:

class Solution {
public:
    int myAtoi(string str) {
        int i = 0;
        long long int res = 0;
        bool flag = false;
        //long long 8个字节可以解决int溢出的问题
        const long long max = 0x100000000;
        //用双引号和单引号都可以表示空格符,无差,最好用单引号表示单个字符
        while(i < str.length() && str[i] == ' ')
            i++;
        if(str[i] == '+')
            i++;
        //因为例子中有一个+-2要求输出为0,所以要用if-else对一个+-进行判断
        //两个+-号是错误的
        else if(str[i] == '-')
        {
            i++;
            flag = true;
        }
        for(; i < str.length(); i++)
        {
            if(str[i] >= '0' && str[i] <= '9')
            {
                res = res * 10 + str[i] - '0';
                if(res > max)
                {
                    res = max;                   
                }               
            }
            //不能有不是数字的字符
            else
                break;
        }
        if(flag == true)
            res = -res;
        if(res < INT_MIN)
            res = INT_MIN;
        if(res > INT_MAX)
            res = INT_MAX;
        return res;
        
    }
};

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值