String to Integer: strings turned to plastic

Source: Internet
Author: User
Tags empty integer range valid

[Question:]

Hint:carefully consider all possible input cases. If you are want a challenge, please don't be below and ask yourself what are input possible.

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

Requirements for Atoi:

The function A-discards as many whitespace characters as necessary until the ' the ' non-whitespace is character. Then, starting from this character, takes a optional initial plus or minus sign followed by as many numerical as P Ossible, and interprets them as a numerical value.

The string can contain additional characters after those that form the integral number, which are ignored and have no Effe CT on the behavior's this function.

More Wonderful content: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/

If the sequence of non-whitespace characters in Str are not a valid integral number, or if no such sequence Cause either str is empty or it contains only whitespace characters, no conversion is performed.

If No valid conversion could are 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.

[Analysis:]① string is empty or empty string, return 0;② ignore the space before and after the string; ③ the first character is ' + ' or '-' remember it first, then read down, and if you read a number, start working with numbers, and if you have other special characters, stop reading and return the results; ④ processing, if you convert Value exceeds the range of type int, the maximum or minimum value of int is returned.
[Solution:]

public class Solution {private final static int int_max = 2147483647;  
     
    Private final static int int_min =-2147483648;  
             
        public int atoi (String str) {int i = 0, flag = 1;//flag is a positive or negative marker long result = 0;//may be long after conversion  
             
        if (str = NULL | | str.length () = = 0) {return 0;//Step 1}  
             
        char[] ch = Str.trim (). ToCharArray (); if (Ch.length = = 0) {return 0;//Step 2} if (ch[i] = = ' + ' | | | ch[i] = = ' -') {flag = ch[i++] = = ' + '? 1:-1;//Step 3} while (I < ch.length &&am P Ch[i] >= ' 0 ' && ch[i] <= ' 9 ') {result = result * + (ch[i++]-' 0 ');//' 0 ' to ' 9 ' askii code is 48~5 7 if (Result * Flag < Int_min | |-result * flag > Int_max) {return result * flag ; Int_min? Int_min:int_max;  
       Step 4     } return (int) result * FLAG;  
    public static void Main (string[] args) {System.out.println (New Solution (). Atoi ("999")); }  
}

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.