Java version of leetcode08-string to Integer (atoi)

Source: Internet
Author: User

My Leetcode journey, this chapter mainly completes the use of Java implementation algorithm. This is the 8th piece of string to Integer (atoi)


All code Download: GitHub Link: GitHub link, click Surprise, write article is not easy, welcome everyone to take my article, and give useful comments, of course you can also pay attention to my GitHub;

1. Topic Description:

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.

Update (2015-02-10):
The signature of the C++ function had been updated. If you still see your function signature accepts a const char * argument, please click on the Reload button to reset your Code definition.

Spoilers alert ... click to show requirements for atoi.

Requirements for Atoi:

The function first discards as many whitespace characters as necessary until the first non-whitespace character is found. Then, the starting from this character, takes a optional initial plus or minus sign followed by as many numerical digits as P Ossible, and interprets them as a numerical value.

The string can contain additional characters after those that form the integral number, which is ignored and has no Effe CT on the behavior of this function.

If the first sequence of non-whitespace characters in STR isn't a valid integral number, or if no such sequence exists be Cause either str is empty or it contains only whitespace characters, no conversion is performed.

If No valid conversion could be performed, a zero value is returned. If the correct value is out of the range of representable values, Int_max (2147483647) or int_min ( -2147483648) is Returne D.

2. My train of thought:

1. The topic is very easy, mainly for some special output to consider
2. Details of the special situation see my comments

3. My AC Code
 PackageCom.rlovep.int1;/** * String to Integer (atoi) * My idea: * The topic is very easy, mainly for some special output to consider * special status details see my comments * @author Peace * */ Public  class Atoi {      Public  Static int Myatoi(String str) {Longres=0;//Use long type mainly to prevent exceeding the limit of int         Longout=2147483648L//When RES technology is greater than out, it proves to be excessive and can end the transition to the rest         if(str==NULL||"". Equals (str))return 0;//for string null and NULL considerations         intI=0;//Record character position indexStr=str.trim ();//Remove the opening and closing whitespace characters         intLength=str.length ();//Get string length         Booleanflag=true;if(Str.charat (i) = =' + '|| Str.charat (i) = ='-'){if(length<2)return 0;//When the string has only one sign bit, return directly             if(Str.charat (i) = ='-') flag=false;//positive and negative signi++; } for(; i<length;i++) {intC=str.charat (i)-' 0 ';//Get transformed integers             if(c>=0&&c<=9) {res=res*Ten+c;//To convert                  if(res>out) Break;//Determine if excessive}Else{ Break;//Illegal characters appear, end transformation}         }if(flag==false) Res=-res;if(res>2147483647) res=2147483647;//Positive excess output positive maximum value         if(res<-2147483648) res=-2147483648;///negative excess output negative maximum value         return(int) Res; } Public Static void Main(string[] args) {System.out.println (Myatoi ("9223372036854775809")); }}

Ok this chapter is introduced here from the Wpeace (blog.wpeace.cn)

Java version of leetcode08-string to Integer (atoi)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.