[Leetcode] [8] String to Integer (atoi) parsing and imitating Java source Code implementation-java implementation

Source: Internet
Author: User

Q:

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.

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.


A:
The following solutions and code did not borrow any previous information, if there is a better solution please leave a comment in the comments section

The idea is to let us implement Atoi, that is, to convert a string into a digital return, in Java we directly string.valueof () just fine. There are several hint say to be aware of all the possibilities of input. And then look down there's a Requirements for atoi:

1, there may be spaces

2, plus sign, and may have more than one sign

3, null value

4. Out of int range

5, no display content returned 0, out of range display Int_max and int_min

I laughed as soon as I read the question ... is not the source code that we said yesterday? Yesterday we used a bit operation in the middle of the reference to the previously seen part of the source code. It was just yesterday, from integer to string. Today is the reverse, but it's a lot to ask today.

In Java source code, roughly first verify that the first character is positive or negative, not the words or only the sign to run abnormal, and then began to traverse Ah, transformation ah. The focus is on the transformation. We can use switch for common conversions. But the source is not yo. For 128 to 127 is the cache good number, in addition to do a stirng to int conversion, in this int conversion to the char directly into int and then to the operation of Unicode, this is dug too deep, we do not need to delve into, Do some swtich conversion.

The code is as follows:

public class Stringtointeger {public static void main (string[] args) {System.out.println ("   -  94dsad564as "));} public static int method (String s) {int i = 0;int digit = 0;int result = 0;char[] cs = S.tochararray (); int negative = 0;//0 Unassigned, 1 negative, 2 positive while (i<cs.length) {if (cs[i]== ' + ')//plus sign {if (negative! = 0) {return 0;} negative = 2;} else if (cs[i]== '-') {//minus sign if (negative! = 0) {return 0;} negative = 1;} else if (cs[i]>=48&&cs[i]<=57) {//digital digit = (int) Cs[i]-48;result = (result<<3) + (result<<1) + digit;//*10;} i++;} Return (negative==1)-result:result;}}
Although the algorithm is not as wonderful as the previous several times, but still a bit of meaning

[Leetcode] [8] String to Integer (atoi) parsing and imitating Java source Code implementation-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.