class Solution
{
public:
/*You are required to complete this method */
int atoi(string A)
{
//Your code here
int res=0;
int cnt=0;
for(int i=0; i<A.size();i++)
{
int c=(A[i]-'0');
if(c==-3)
{
cnt++;
}
else if(c>=0 && c<=9)
{
res=res*10+(A[i]-'0');
}
else return -1;
}
if(cnt>1)
{
return -1;
}
if(cnt==1)
{
cout<<"-";
}
return res;
}
};
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter