[Leetcode] [JavaScript] String to Integer (atoi)

Source: Internet
Author: User

String to Integer (atoi)

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.

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.

Boring problem, when I first asked me to do such a problem when I was refused.

Notice what time it is:

1. There is a positive sign in front;

2.-12A12 output-12;

3.int_max (2147483647), Int_min (-2147483648);

4. There is a long integer, all turn into numeric will be out of bounds, over the extremum can return the results.

1 /**2 * @param {string} str3 * @return {number}4  */5 varMyatoi =function(str) {6str =Str.trim ();7     vari = 0; Sign = ' + ', num = "", result = 0, base = 1;8     if(Str[0] = = = ' + '){9i++;Ten}Else if(Str[0] = = = '-'){ OneSign = '-'; Ai++; -     } -      for(; i < str.length; i++){ the         if(str[i].charcodeat (0) >= && str[i].charcodeat (0) <= 57){ -num + =Str[i]; -}Else{ -             if(num = = ""){ +                 return0; -}Else{ +                  Break; A             }             at         } -     } -      for(i = num.length-1; I >=0; i--){ -Result + = base *Num[i]; -Base *= 10; -         if(Result > 2147483647 && sign = = = ' + ')){ in             return2147483647; -}Else if(Result > 2147483648 && sign = = = '-')){ to             return-2147483648; +         } -     } the     returnSign = = = '-'? Result *-1: result; *};

Direct modulation system Function Revenge Society

1 /**2 * @param {string} str3 * @return {number}4  */5 varMyAtoi2 =function(str) {6     varres =parseint (str);7     if(Res > 2147483647){8res = 2147483647;9}Else if(Res <-2147483648){Tenres =-2147483648; One     } A     returnIsNaN (res)? 0: res; -};

[Leetcode] [JavaScript] 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.