[Inscription] string integer conversion -leetcode

Title: integer string conversion

 

Atoi you to implement a function, it can convert a string to an integer.

First, the function will be based on discards need to begin with a space character useless until the find to the first non-space character so far. The following conversion rules are as follows:

  1. If the first character is a non-null positive or negative number, the sign of the back with as many consecutive numeric characters are combined to form a signed integer.
  2. If the first non-space character is a number, which is directly after the continuous numeric characters are combined to form an integer.
  3. The string may also exist after the integer part of the effective extra characters, these characters can be ignored, they should not affect the function.

Note: When the string if the first non-space character is not a valid integer character string is empty or contains only whitespace character string, the function does not require you to convert that conversion can not be effective.

In any case, if the function can not effectively convert, 0 is returned.

prompt:

This question blank characters include only the space character ''.
We assume that the environment can store 32-bit signed integer size, then the numerical range [-2 31 is 2 31 is  - 1] If the value exceeds this range, return INT_MAX (2 31 is  -. 1) or INT_MIN (-2 31 is ).
 

Example 1:

Input: "42"
Output: 42


Example 2:

Input: "-42"
Output: -42
Explanation: a first non-blank character '-', it is a negative sign.
  We will all digital consecutive negative number and later combined as much as possible, and finally get -42.


Example 3:

Input: "4193 with words"
Output: 4193
Explanation: converting the digital OFF '3', because the next character is not numeric.


Example 4:

Input: "words and 987"
Output: 0
Explanation: a first non-blank character 'w', but it is not a positive number or negative number.
Therefore, the conversion can not be performed effectively.


Example 5:

Input: "-91283472332"
Output: -2147483648
explanation: the number "-91283472332" Over the range of 32-bit signed integer.
  Accordingly return INT_MIN (-2 31 is ).


The questions were very clear let us convert a string to an integer.

The difficulty is that exceed the plastic range.

 

We roughly divided into the following steps to resolve this problem Road:

  1. Omit useless spaces
  2. Judge symbol
  3. Digital processing

In dealing with numbers, we need to determine whether the integer integer beyond the scope:

  • ans*10 + v > INT_MAX

But the expression on the left and the * 10 + v is also likely to be greater than integer range, so we make the appropriate variant:

  • ans > ( INT_MAX - v ) / 10

Thus, it can be determined without departing from the scope of the shaping.

 

The codes (c ++):

class Solution {
public:
    int myAtoi(string str) {
        int i = 0,sign = 1;
        int ans = 0;

        // skip spaces 
        the while (STR [I] == '  ' ) ++ I;

        // processed symbols 
        IF (STR [I] == ' - ' ) = Sign - . 1 ;
         IF (STR [I] == ' + ' || STR [I] == ' - ' ) I ++ ;

        //处理数字
        while ( str[i] >= '0'&& str[i] <= '9' ) {
            int v = str[i] - '0';
            if ( ans > ( INT_MAX - v ) / 10 ) return sign == 1? INT_MAX:INT_MIN;
            years = years * 10 + v;
            i++;
        }

        return sign*ans;
    }
};

2020-04-03-19:48:59

 

Guess you like

Origin www.cnblogs.com/Sxccz/p/leetcode_14.html