Leetcode-8 (Java) 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 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.

Portal: https://leetcode.com/problems/string-to-integer-atoi/

Convert the string to Integer, and note the spaces and symbols.

 Public classSolution { Public intmyatoi (String str) {if(str = =NULL)            return0; STR= Str.trim ();//remove the spaces in front of the string        intLen =str.length (); if(len = = 0)            return0; CharSignal = ' + '; Doubleres = 0;  for(inti = 0; I < str.length (); i++)        {            CharCH =Str.charat (i); if(ch = = '-' | | ch = = ' + '))            {                if(I! = 0)                    return0; Else                {                    if(ch = = '-')) Signal= '-'; }            }            Else if(Ch >= ' 0 ' && ch <= ' 9 ') {res= res * + CH-' 0 '; }            Else                 Break; }        if(Signal = = '-')) Res= -Res; if(Res >integer.max_value)returnInteger.max_value; Else if(Res <integer.min_value)returnInteger.min_value; Else            return(int) Res; }    }

Leetcode-8 (Java) 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.