Leecode #8 String to Integer (atoi)

一、 问题描述
Leecode第八题,题目为:

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.

问题理解为

函数首先去掉空格,直到找到第一个非空格字符。然后,从第一个非空格字符开始,取一个可选的初始正负号,后跟尽可能多的数字,将其转换为数值。
字符串可以包含整数的字符以外的字符,这些字符将被忽略,并且对函数功能没有影响。
如果str中的第一个非空格字符序列不是有效的整数,或者由于str为空或只包含空格字符而不存在这样的序列,则不执行转换函数。
如果不能执行有效的转换,则返回零值。
注:
1、只有字符’ '被认为是空白字符。
2、假设我们处理的环境只能存储32位带符号整数范围内的整数:[- 2^31, 2^31- 1]。如果数值超出可表示值的范围,则返回INT_MAX(231 - 1)或INT_MIN(- 231)。

例 1:

输入: “42”
输出: 42

例 2:

输入: " -42"
输出: -42
注释: 第一个非空格字符为 ‘-’, 表示减号。.
根据取尽可能多的数字的原则,我们得到数值42.

例 3:

输入: “4193 with words”
输出: 4193
注释: 转换在数字“3”处停止,因为下一个字符不是数字.

例4:

输入: “words and 987”
输出: 0
注释:第一个非空格字符是“w”,它不是一个数字
数字或正负号。因此,不能执行任何有效的转换。

例 5:

输入: “-91283472332”
输出: -2147483648
注释: 数字“-91283472332”超出了32位带符号整数的范围。
返回前INT_MIN(- 231)。

二、解题思路

1、检查空格和字符
2、找出字符串中的数字然后返回。

三、实现代码

class Solution {
public:
    int myAtoi(string str) {
        long res = 0;
        int sign = 1;
        int i = 0;
        
        	while(i < str.length() && str[i] == ' ') i++;
        	if(str[i] == '-' || str[i] == '+'){
        		sign = (str[i++] == '-') ? -1 : 1;
			}
			while(i < str.length() && '0' <= str[i] && str[i] <= '9')
			{
				res = res * 10 + (str[i++] - '0');
				if(res * sign >= INT_MAX) return INT_MAX;
            	if(res * sign <= INT_MIN) return INT_MIN; 
			}
	
		return res * sign;
    }
};

猜你喜欢

转载自blog.csdn.net/serena_t/article/details/90115778
今日推荐