table of Contents:
- topic
- Script and notes
- Script Logic
topic:
Atoi you to implement a function, it can convert a string to an integer.
First, the function will begin with a space character discard useless if necessary, until the find to the first non-space character so far.
When we find the first non-space character is a positive or negative number, the combination of the symbols as much as possible with consecutive numbers up later, as the sign of integer; if the first non-space character is figures, which directly after the continuous numeric characters are combined to form an integer.
In addition to the string after a valid integer part may also exist extra characters, these characters can be ignored, they should not affect a function.
Note: if the character string in the first non-space character is not a valid integer character string is empty or contains only white space character string, then you will not need to be a function of conversion.
In any case, if the function can not effectively convert, 0 is returned.
Description:
We assume that the environment can store 32-bit signed integer size, then the numerical range [-231 231--1]. If the value exceeds this range, return INT_MAX (231 - 1) or INT_MIN (-231).
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.
So return INT_MIN (-231).
Script and notes: [time-consuming: 32ms]
class Solution: def myAtoi(self, str: str) -> int: str2 = str.strip() n1 = len (str2) tar1 = "" if n1 == 0: return(0) else: for i in range(n1): if str2[i].isdigit(): tar1 += str2[i] elif str2[i] == "+" or str2[i] == "-": if tar1 == "": tar1 += str2[i] else: break else: break try: tar2 = int(tar1) if tar2 > 2 ** 31 - 1 or (-2)**31 > tar2: if 0 > tar2: return((-2)**31) else: return(2**31 -1) else: return(tar2) except ValueError: return(0)
Script logic:
- This title belongs to the conditions of screening questions: the number of cases, the selection criteria in line with the In view of the understanding of the subject: traversal of a given character string added to the variable number, if encounter the following situation is stopped:
- If the character is a word, then stop the walk, the storage variable to judge
- If you encounter the "+" or "-" symbol is judged based on the variable storage case, if the memory variable is empty, then add the characters; otherwise stop the walk
- ...
- After traversing completed, another step is to judge and deal with overflow