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-problem to be specified vaguely (ie, no given input specs). You is responsible to gather all the input requirements up front.
Portal: https://leetcode.com/problems/string-to-integer-atoi/
Convert the string to Integer, and note the spaces and symbols.
Public classSolution { Public intmyatoi (String str) {if(str = =NULL) return0; STR= Str.trim ();//remove the spaces in front of the string intLen =str.length (); if(len = = 0) return0; CharSignal = ' + '; Doubleres = 0; for(inti = 0; I < str.length (); i++) { CharCH =Str.charat (i); if(ch = = '-' | | ch = = ' + ')) { if(I! = 0) return0; Else { if(ch = = '-')) Signal= '-'; } } Else if(Ch >= ' 0 ' && ch <= ' 9 ') {res= res * + CH-' 0 '; } Else Break; } if(Signal = = '-')) Res= -Res; if(Res >integer.max_value)returnInteger.max_value; Else if(Res <integer.min_value)returnInteger.min_value; Else return(int) Res; } }
Leetcode-8 (Java) String to Integer (atoi)