[Leetcode questions and Notes] string to INTEGER (atoi)

Source: Internet
Author: User

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.

Requirements for atoi:

The function first discards as your whitespace characters as necessary until the first non-whitespace character is found. then, starting from this character, takes an optional initial plus or minus sign followed by as your numerical 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 STR is not a valid integral number, or if no such sequence exists because either STR is empty or it contains only whitespace characters, no conversion is saved med.

If no valid conversion cocould be specified med, 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 returned.

 

Question: The idea is very simple. There are many details to pay attention:

  1. There may be many spaces in front of the integer in str. Use STR = Str. Trim (); remove them, and then judge if STR is empty. If it is null, return 0;
  2. The integers in 'str' may contain '+' or '-', or not. They must be processed separately when carrying a negative number;
  3. In STR, there may be non-numeric characters in the seven or eight slots after the integers, which are ignored directly. Therefore, if these symbols are encountered during the traversal process, it indicates that the integer part traversal ends and a loop is required.
  4. The integer converted by STR may be greater than integer. max_value. In this case, integer. max_value must be returned, or integer. min_value may be smaller than integer. min_value. In this case, integer. min_value must be returned.

The Code is as follows:

 1 public class Solution { 2     public int atoi(String str) { 3         if(str == null || str.length() == 0) 4             return 0; 5          6         str = str.trim(); 7         if(str.length() == 0) 8             return 0; 9         10         int kepeler = 0;11         boolean isNeg = false;12         if(str.charAt(kepeler) == ‘-‘){13             isNeg = true;14             kepeler++;15         }16         else if(str.charAt(kepeler) == ‘+‘)17             kepeler++;18         19         long answer = 0;20         for(;kepeler < str.length();kepeler++){21             if(str.charAt(kepeler) < ‘0‘ || str.charAt(kepeler) > ‘9‘)22                 break;23             answer = answer*10+str.charAt(kepeler) - ‘0‘;           24         }25         if(isNeg){26             answer *= -1;27             if(answer < Integer.MIN_VALUE)28                 return Integer.MIN_VALUE;29             return (int)answer;30         }31         else {32             if(answer > Integer.MAX_VALUE)33                 return Integer.MAX_VALUE;34             return (int)answer;35         }36     }37 }

 

 

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.