[Leetcode] string to INTEGER (atoi)

Source: Internet
Author: User
Problem description:

Implement atoi to convert a string to an integer.

Hint:Carefully consider all possible input cases. If you want a challenge, please do not see below and ask yourself what are the possible input cases.

Notes:It is intended for this problem to be specified vaguely (ie, no given input specs). You are responsible to gather all the input requirements up front.


Ideas:

It is not difficult to convert a string to an integer of the int type, but there are many input situations that need to be considered, which may easily cause omissions. First, let's take a look at the definition of atoi in C ++:

-----------------------------------

Atoi
int atoi (const char * str);
Convert string to integerparses the C-string StrInterpreting its content as an integral number, which is returned as a value of Type int.

The function first discards as Alias whitespace characters (as in Isspace) As necessary until the first non-whitespace character is found. Then, starting from this character, takes an optional initial PlusOr MinusSign followed by as your base-10 digits as possible, and interprets them as a numerical value.

The string can contain in additional characters after those that form the integral number, which are ignored and have no effect on the behavior of this function.

If the first sequence of non-whitespace characters in StrIs not a valid integral number, or if no such sequence exists because either StrIs empty or it contains only whitespace characters, no conversion is already med and zero is returned.


Return valueon success, the function returns the converted integral number as intValue.
If the converted value wocould be out of the range of representable values by int, It causes Undefined behavior. See StrtolFor a more robust cross-platform alternative when this is a possibility.

------------------------------------------

Note the following points:

  1. Remove leading space
  2. Note the symbols at the beginning +-
  3. Ignore the miscellaneous characters behind the number
  4. Consider overflow.

After considering the above aspects, the code can be AC.


Code:

public class String_To_Integer { //javapublic static int atoi(String str) {int haveMinus = 1;long result = 0;  if(str == null || str.trim().isEmpty())return 0;//chech + - str = str.trim();if(str.charAt(0) == '-'){str = str.substring(1);haveMinus = -1;}else if(str.charAt(0) == '+')str = str.substring(1);//check numfor(int i = 0;i < str.length() && str.charAt(i) >= '0' && str.charAt(i) <= '9'; i++){result = result*10 + (str.charAt(i)- '0');}//deal overflowif(result > 2147483647 && haveMinus == 1)return 2147483647;if(result > 2147483647 && haveMinus == -1)return -2147483648;return haveMinus*(int)result;    }public static void main(String [] args){System.out.println(String_To_Integer.atoi("2147483648"));}}


[Leetcode] string to INTEGER (atoi)

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.