-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathImplement Atoi.cpp
60 lines (52 loc) · 1.21 KB
/
Implement Atoi.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
60
//{ Driver Code Starts
//Initial template for C++
#include <bits/stdc++.h>
using namespace std;
// } Driver Code Ends
//User function template for C++
class Solution{
public:
/*You are required to complete this method */
int atoi(string s) {
//Your code here
int count =0;
int index=-1;
int count2=0;
int index2=-1;
for(int i=0;i<s.length();i++){
if(s[i]=='-' && index==-1) count++,index=i;
else if(s[i]>='0' && s[i]<='9') continue;
else return -1;
}
if(count>1) return -1;
if(count==1 && index!=0) return -1;
int maxi=INT_MAX;
int ans=0;
long long temp=0;
for(int i=0;i<s.length();i++){
if(s[i]=='-' || s[i]=='+') continue;
else {
temp=temp*10+s[i]-'0';
//if(temp>INT_MAX || temp<INT_MIN) return -1;
//else
ans=(int)temp;
}
}
if(s[0]=='-') ans*=-1;
return ans;
}
};
//{ Driver Code Starts.
int main()
{
int t;
cin>>t;
while(t--)
{
string s;
cin>>s;
Solution ob;
cout<<ob.atoi(s)<<endl;
}
}
// } Driver Code Ends