-
Notifications
You must be signed in to change notification settings - Fork 0
/
implementAtoi.cpp
49 lines (44 loc) · 1.05 KB
/
implementAtoi.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
// { 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 str) {
//Your code here
string temp;
int ans=-1;
temp.push_back(str[0]);
bool flag=0;
if(temp.compare("-")==0) flag=1;
if(!flag){
if(!(temp.compare("9")<=0 && temp.compare("9")>=-9)) return -1;
}
for(int i=1; i<str.length(); ++i) {
temp = "";
temp.push_back(str[i]);
if(!(temp.compare("9")<=0 && temp.compare("9")>=-9)) return -1;
}
if(flag) str = str.substr(1,str.length());
stringstream ss(str);
ss >> ans;
if(flag) 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