Leetcode-8 String to Integer (atoi)

Source: Internet
Author: User



Problem Description:

Implement atoi To Convert a string to an integer.

Hint: Carefullyconsider all possible input cases. If you want a challenge, please don't seebelow and ask yourself what is the possible input cases.

Notes: It isintended 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++ Functionhad been updated. If you still see your function signature accepts a const char * argument, please click the reload buttonto reset your code definition.

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

Requirements for Atoi:

The Functionfirst discards as many whitespace characters as necessary until the firstnon-whitespace character is found. Then, starting from the character, takes anoptional initial plus or minus sign followed by as many numerical digits Aspos Sible, and interprets them as a numerical value.

The Stringcan contain additional characters after those that form the integral Number,which is ignored and has no effect The behavior of this function.

If the firstsequence of non-whitespace characters in Str are not a valid integral number, Orif No such sequence exists Beca Use either str are empty or it contains onlywhitespace characters, no conversion is performed.

If no validconversion could be performed, a zero value is returned. If the correct valueis out of the range of representable values, Int_max (2147483647) or int_min ( -2147483648) is returned.

Code: Mainly consider a variety of situations can:

Refer to C + + atoi:http://blog.csdn.net/woliuyunyicai/article/details/44101881

public class Solution {public int atoi (String str) {if (str = = NULL | | str.length () = = 0) {return 0;} char[] Str_arr = Str.tochararray (); Boolean flag = true;//flag Positive negative number, positive number is truelong result = 0;int index = 0;final long Max_int = (long) (Math.pow (2, 31)-1);//In Java you can use Integer.max_value instead of the final long Min_int = (long) (-(Math.pow (2))); INT data_length = 0 ;//record length, prevent long also overflow case//Consider the case that the preceding character is a space, such as "010", eliminate the whitespace can use String.Trim () while (Index < str.length () && str_arr[ Index] = = ") {index++;} First determine whether the first character is a positive or negative "+,-", Mark if (str_arr[index] = = ' + ') {flag = True;++index;} else if (str_arr[index] = = '-') {flag = False;++index;} Is ' 0 '-' 9 ' character while (Index < str.length () && Str_arr[index] <= ' 9 ' && Str_arr[index] >= ' 0 ' & & data_length++ <=)//data_length is used to control the number string too long {result = result * + (str_arr[index++]-' 0 ');} determine if int crosses result = flag? Result:-result;if (Result > Max_int) {result = Max_int;} else if (Result < Min_int) {result = Min_int;}  return (int) result;   } public static void Main (string[] args) {//test case//String str = "+004500";//String str = "-0012    A42 ";//String str =" 2147483648 ";//String str =" 2147483648 ";    String str = "9223372036854775809";    SYSTEM.OUT.PRINTLN (New Solution (). atoi (str)); }}

Leetcode-8 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.