-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathatoi.cpp
More file actions
45 lines (42 loc) · 1.14 KB
/
atoi.cpp
File metadata and controls
45 lines (42 loc) · 1.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
// https://leetcode.com/problems/string-to-integer-atoi/
#include<bits/stdc++.h>
using namespace std;
int myAtoi(string s){
if(s=="")return 0;
long int ans=0;
int sign=1;
bool intFound=false;
bool signFound=false;
for(int i=0;i<s.length();i++){
if(s[i]==' '){
if(intFound || signFound)break;
else{
continue;
}
}
if(s[i]=='-' || s[i]=='+'){
if(intFound || signFound==true)break;
else{
signFound=true;
sign=(s[i]=='-')?-1:1;
}
}
else if(s[i]-'0'>=0 && s[i]-'0'<=9){
intFound=true;
ans=ans*10;
if(ans>=INT_MAX)return (sign==-1)?INT_MIN:INT_MAX;
ans+=(s[i]-'0');
// if(ans==2147483647 && sign==-1)return -2147483647;
if(ans==INT_MAX && sign==-1)return -1*INT_MAX;
if(ans>=INT_MAX)return (sign==-1)?INT_MIN:INT_MAX;
}
else if(s[i]-'0'<0 || s[i]-'0'>9){
break;
}
}
return sign*ans;
}
int main(){
cout<<myAtoi("42");
return 0;
}