The description of the question is in the following link:
https://leetcode.com/problems/string-to-integer-atoi/
My Code:
def myAtoi(self, String) -> int:
if(String == ""):
return(0);
else:
while(String[0] == " "):
String = String[1:len(String)];
if(String == ""):
return(0);
sign = 1;
if(String[0] == "-"):
sign = -1;
String = String[1:len(String)];
else:
if(String[0] == "+"):
String = String[1:len(String)];
Number = ""
i = 0;
while(i 2**31-1):
return(2**31-1);
else:
if(Number < -2**31):
return(-2**31);
else:
return(Number);
Explanation:
The idea is clear. First part is to discard all the possible space. What we need to be very careful is the empty string. The second part is to determine if there is any potential sign for the number. The last part is to take all the available number string and then do the conversion.