You are given a string ‘s’ of length 'n'.
Implement the atoi(string s) function, which converts a given string into a 32-bit signed integer, following similar principles as the atoi function in C/C++.
Here's the step-by-step algorithm for myAtoi(string s):
1. Discard any leading whitespaces.
2. If the next character (if not at the end of the string) is '-' or '+', consider it to determine the sign of the result. If neither is present, assume the result is positive.
3. Read and accumulate digits until a non-digit character is encountered or the end of the input is reached.
4. Convert the collected digits into an integer (e.g., "123" becomes 123, "0032" becomes 32). If no digits were read, the integer is 0. Adjust the sign as needed (as determined in step 2).
5. If the integer falls outside the range of a 32-bit signed integer [-2^31, 2^31 - 1], constrain it to stay within the range. For instance, if the integer is less than -2^31, set it to -2^31; if it's greater than 2^31 - 1, set it to 2^31 - 1.
6. Return the resulting integer.
1. Only the space character ' ' is treated as a whitespace.
2. All characters other than leading whitespace or digits are considered.
Input: 45rohit12
Output: 45
Explanation:
Leading Whitespace: Leading whitespace characters (" ") are ignored.
Numeric Portion: The numeric portion starts with the digit "4".
Reading Numeric Characters: The algorithm reads "4" and "5" to form "45".
End of Numeric Portion: The numeric portion ends at "r", non-numeric characters are skipped.
Parsing Result: "45" is parsed into the integer value 45, the final result.
The only line of the test case contains the string ‘s’.
Output format :
The output contains the integer formed by the given string.
Note:
You do not need to print anything. It has already been taken care of. Just implement the given function.
-546+er
-546
Leading Whitespace: Leading whitespace characters (" ") are ignored.
Sign Detection: The "-" sign indicates a negative result.
Numeric Portion: The numeric portion begins with the digit "5".
Reading Numeric Characters: The algorithm reads "5", "4", and "6" to form "546".
End of Numeric Portion: The numeric portion ends at "+".
Parsing Result: "546" is parsed into the integer value 546. Due to the negative sign, the final result is -546.
-ban23
0
0 <= n <= 200
's' consists of English letters (lower-case and upper-case), digits (0-9), ' ', '+', '-', and '.'.
Try to solve the problem Iteratively.
Approach:
The approach involves parsing a string to convert it into a 32-bit signed integer. The algorithm starts by skipping leading whitespaces. It detects positive or negative signs and accumulates the numeric portion of the string. By converting characters to integers, it forms the integer value. The approach handles cases of negative signs, sign clashes, and integer range limits. The result is returned as the final output.
Algorithm :
FUNCTION createAtoi(s: string) ->
O(N), where ‘N’ denotes the length of the string.
We traverse the string once.
Therefore the overall time complexity will be O(N).
O(1)
We are not using any extra space.
Therefore overall space complexity will be O(1).