forked from ianshulx/DSA-Programs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
String_To_Integer.cpp
59 lines (53 loc) · 1.15 KB
/
String_To_Integer.cpp
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include<bits/stdc++.h>
using namespace std;
class Solution {
public:
int myAtoi(string s) {
long long int r=0,sign=1,i=0;
while(1)
{
if(s[i] == ' ')
i++;
else
break;
}
if(s[i]=='-')
{
sign = -1;
if(int(s[i+1])>=48&& int(s[i+1]) <=57)
i++;
else return 0;
}
if(s[i]=='+')
{
if(int(s[i+1])>=48 && int(s[i+1]) <=57)
i++;
else return 0;
}
while(1)
{
if(s[i]=='0')
i++;
else break;
}
while(1)
{
if(int(s[i])>=48&& int(s[i])<=57)
r = r*10 + int(s[i++])-48;
else break;
long long int c = r*sign;
if(c< (-1)*(pow(2,31)))
return (-1)*(pow(2,31));
if(c>pow(2,31)-1)
return pow(2,31)-1;
}
r*= sign ;
return r;
}
};
int main(){
Solution s ;
int result= s.myAtoi("45");
cout<<result ;
return 0;
}