Implement the myAtoi(string s) function, which converts a string to a 32-bit signed integer (similar to C/C++'s atoi function) #178
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Implement the myAtoi(string s) function, which converts a string to a 32-bit signed integer (similar to C/C++'s atoi function)
Pull Request Template
Description
The algorithm for myAtoi(string s) is as follows:
Read in and ignore any leading whitespace.
##Summary
1.Check if the next character (if not already at the end of the string) is '-' or '+'. Read this character in if it is either. This 2.determines if the final result is negative or positive respectively. Assume the result is positive if neither is present.
3.Read in next the characters until the next non-digit charcter or the end of the input is reached. The rest of the string is ignored.
4.Convert these digits into an integer (i.e. "123" -> 123, "0032" -> 32). If no digits were read, then the integer is 0. Change the sign as necessary (from step 2).
5.If the integer is out of the 32-bit signed integer range [-231, 231 - 1], then clamp the integer so that it remains in the range. Specifically, integers less than -231 should be clamped to -231, and integers greater than 231 - 1 should be clamped to 231 - 1.
6.Return the integer as the final result.
##Approach
Approach
1.Prepare String that is having only +- sign and number value
2.Convert to BigInteger to compare value with 32bit signed Integer range and clamp if goes out of range
3.return output with 0 if no string number found or non number value found before any number value
Put check marks:
Have you made changes in README file ?
How Has This Been Tested?
Please describe the tests that you ran to verify your changes. Please also note any relevant details for your test configuration.
1.This solution has been tested with set of testcases on Leetcode website itself.
2.I personally tested with set of values as below:
"+-12"
"-a33"
"2000000000000"
" -45"
"+12"
"123abc"
Make sure all below guidelines are followed else PR will get Reject: