Skip to content

Implement the myAtoi(string s) function, which converts a string to a 32-bit signed integer (similar to C/C++'s atoi function) #178

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 2, 2021

Conversation

sachsbu
Copy link
Contributor

@sachsbu sachsbu commented Jul 2, 2021

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 ?

  • [ Done] Added problem & solution under correct topic.
  • [ Done] Specified Space & Time complexity.
  • [ Done] Specified difficulty level, tag & Note(if any).

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:

  • [ Done] My code follows the style guidelines of this project
  • [ Done] I have performed a self-review of my own code
  • [ Done] I have commented my code so that it is easy to understand
  • [ Done] I have made corresponding changes to the documentation
  • [ Done] My changes generate no new warnings
  • [ Done] Any dependent changes have been merged and published in downstream modules

sachsbu added 2 commits July 2, 2021 18:29
Implement the myAtoi(string s) function, which converts a string to a 32-bit signed integer (similar to C/C++'s atoi function)
Rename Java file as per standard and add details to .md file
@welcome
Copy link

welcome bot commented Jul 2, 2021

I can tell this is your first pull request! Thank you I'm so honored. 🎉🎉🎉 I'll take a look at it ASAP!

@GouravRusiya30 GouravRusiya30 merged commit 7d9d0a5 into codedecks-in:master Jul 2, 2021
@welcome
Copy link

welcome bot commented Jul 2, 2021

Your code looks great! Congrats, I've gone ahead and merged your first pull request! Keep it up! alt text

@sachsbu
Copy link
Contributor Author

sachsbu commented Jul 3, 2021 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants