- Notifications
You must be signed in to change notification settings - Fork2.4k
Create: 0008-string-to-integer#3991
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Open
Tarun-Manchala wants to merge1 commit intoneetcode-gh:mainChoose a base branch fromTarun-Manchala:main
base:main
Could not load branches
Branch not found:{{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline, and old review comments may become outdated.
Uh oh!
There was an error while loading.Please reload this page.
Open
Changes fromall commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
78 changes: 78 additions & 0 deletionscpp/0008-string-to-integer.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
#include<bits/stdc++.h> | ||
using namespace std; | ||
/* | ||
Implement the myAtoi(string s) function, which converts a string to a 32-bit signed integer. | ||
The algorithm for myAtoi(string s) is as follows: | ||
Whitespace: Ignore any leading whitespace (" "). | ||
Signedness: Determine the sign by checking if the next character is '-' or '+', assuming positivity if neither present. | ||
Conversion: Read the integer by skipping leading zeros until a non-digit character is encountered or the end of the string is reached. If no digits were read, then the result is 0. | ||
Rounding: If the integer is out of the 32-bit signed integer range [-231, 231 - 1], then round the integer to remain in the range. Specifically, integers less than -231 should be rounded to -231, and integers greater than 231 - 1 should be rounded to 231 - 1. | ||
Return the integer as the final result. | ||
Example : | ||
Input: s = " -042" | ||
Output: -42 | ||
Explanation: | ||
Step 1: " -042" (leading whitespace is read and ignored) | ||
^ | ||
Step 2: " -042" ('-' is read, so the result should be negative) | ||
^ | ||
Step 3: " -042" ("042" is read in, leading zeros ignored in the result) | ||
*/ | ||
class Solution { | ||
public: | ||
int myAtoi(string s) { | ||
int sign=1; | ||
bool tag=false; | ||
int j=0,ind; | ||
double ans=0; | ||
// trim the string till we get number | ||
for (int i=0;i<s.size();i++){ | ||
if (s[i]==' ') { | ||
continue;} | ||
else{ | ||
if (s[i]=='-') { | ||
sign =-1; | ||
j=i+1; | ||
break; | ||
} | ||
else if(s[i]=='+'){ | ||
sign=1; | ||
j=i+1; | ||
break; | ||
} | ||
else{ | ||
j=i; | ||
break; | ||
} | ||
} | ||
} | ||
// convert string to integer | ||
for (int i=j;i<s.size();i++){ | ||
if (int(s[i])>47 && int(s[i])<58){ | ||
ans=ans*10; | ||
ans=ans+int(s[i])-48; | ||
} | ||
else { | ||
break; | ||
} | ||
} | ||
ans=sign*ans; | ||
long long y=pow(2,31); | ||
if (ans <-1*y) return -1*y; | ||
else if (ans > y-1) return y-1; | ||
else return ans; | ||
} | ||
}; |
51 changes: 51 additions & 0 deletionspython/0008-string-to-integer.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
''' | ||
Implement the myAtoi(string s) function, which converts a string to a 32-bit signed integer. | ||
The algorithm for myAtoi(string s) is as follows: | ||
Whitespace: Ignore any leading whitespace (" "). | ||
Signedness: Determine the sign by checking if the next character is '-' or '+', assuming positivity if neither present. | ||
Conversion: Read the integer by skipping leading zeros until a non-digit character is encountered or the end of the string is reached. If no digits were read, then the result is 0. | ||
Rounding: If the integer is out of the 32-bit signed integer range [-231, 231 - 1], then round the integer to remain in the range. Specifically, integers less than -231 should be rounded to -231, and integers greater than 231 - 1 should be rounded to 231 - 1. | ||
Return the integer as the final result. | ||
Example : | ||
Input: s = " -042" | ||
Output: -42 | ||
Explanation: | ||
Step 1: " -042" (leading whitespace is read and ignored) | ||
^ | ||
Step 2: " -042" ('-' is read, so the result should be negative) | ||
^ | ||
Step 3: " -042" ("042" is read in, leading zeros ignored in the result) | ||
''' | ||
class Solution: | ||
def myAtoi(self, s: str) -> int: | ||
s=s.strip() | ||
sign=1 | ||
if len(s)==0: | ||
return 0 | ||
if s[0]=="-" or s[0]=="+" : | ||
sign=-1 if s[0]=="-" else 1 | ||
s=s[1:] | ||
l=0 | ||
for c in s: | ||
k=ord(c)-48 | ||
if k in range(0,10): | ||
l=l*10 | ||
l=l+k | ||
else: | ||
break | ||
n=sign*l | ||
if n>=(2**31): | ||
return (2**31)-1 | ||
elif n<(-(2**31)): | ||
return -(2**31) | ||
return n |
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.