Leetcode string to integer Java implementation

Source: Internet
Author: User

Implemented atoi to convert a string to an integer.

The space character in the string needs to be removed before the first non-null character is found. If the first non-null character is a plus or minus sign, select the symbol and combine it with as many consecutive digits as possible, which is the value of the integer. If the first non-null character is a number, it is directly combined with successive numeric characters to form an integer.

Strings can include extra characters after the characters that form integers, which can be ignored and have no effect on the function.

When the first non-null character sequence in a string is not a valid integer, or the string is empty, or the string contains only white space characters, no conversion is made.

Returns 0 if the function cannot perform a valid conversion.

Description

Suppose our environment can store only 32-bit signed integers with a value range of [? 231, 231? 1]. If the value exceeds the range that can be represented, Int_max (231? 1) or Int_min (? 231) is returned.

Example 1:

Input: "42" output: 42

Example 2:

Input: "   42" output:-42 Explanation: The first non-whitespace character is '-', it is a minus sign.    we try to combine the minus sign with all the successive numbers that follow, and finally get-42.

Example 3:

Input: "4193 with words" output: 4193 Explanation: The conversion is due to the number ' 3 ' because its next character is not a number.

Example 4:

Input: "Words and 987" output: 0 Explanation: The first non-null character is ' W ', but it is not a number or positive, minus sign.     Therefore, a valid conversion cannot be performed.

Example 5:

Input: "91283472332" output:-2147483648 Explanation: The number "91283472332" exceeds the 32-bit signed integer range.    so return to Int_min (? 231).
classSolution { Public intmyatoi (String str) {intLength=str.length (); intStart=0; intFactor=1; LongResult=0; StringBuffer SB=NewStringBuffer (); if(Str.length () ==0)            return0;  while(Start<length&&str.charat (start) = = ") {Start++; }        if(start==length)return0; if(Str.charat (start) = = '-' | | Str.charat (start) = = ' + ') {Factor=str.charat (start) = = '-'? -1:1; Start++; }                                 for(inti=start;i<length;i++)        {            intNum=str.charat (i)-' 0 '; if(num<0| | Num>9)                 Break;        Sb.append (Str.charat (i)); }          for(intI=0;i<sb.length (); i++) {result=sb.charat (i)-' 0 ' +result*10; if(result*factor>integer.max_value)returnInteger.max_value; if(result*factor<integer.min_value)returnInteger.min_value; }                return(int) ((int) factor*result);}}

Submit Results

Leetcode string to integer Java implementation

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.