Implement atoi to convert a string to an integer.
Hint: Carefully consider all possible input cases. If you want a challenge, please don't see below and ask yourself what is the possible input cases.
Notes: It is intended for the problem to be specified vaguely (ie, no given input specs). You is responsible to gather all the input requirements up front.
Class Solution {Public:int atoi (const char *str) {string temp= ""; int loc=0; while (Str[loc]! = ' + ') {if (str[loc]!= ') break; loc++; } Long long res=0; if (str[loc] = = '-') {while (Str[loc]! = ')} {if (Str[loc]! = ' 0 ') Break loc++; } if (str[loc] = = ' + ') return 0; for (int i=loc+1; str[i]! = ' str[i '; i++) {if (str[i]<48 | |] >) BR Eak res = res*10+str[i]-48; if (Res-1 > Int_max) return int_min; } if (Res-1 > Int_max) return int_min; Return-res; } else {while (Str[loc]! =) {if (Str[loc]! = ' 0 ') Break loc++; } if (str[loc] = = ' + ') return 0; if (str[loc] = = ' + ') loc = loc+1; for (int i=loc; str[i]!= ' + '; i++) {if (str[i]<48 | | str[i] >) break; res = res*10+str[i]-48; if (res > Int_max) return int_max; } if (res > Int_max) return int_max; return res; } }};
Leetcode--string to Integer (atoi)