Leetcode:string to Integer (atoi)

Source: Internet
Author: User

String to Integer (atoi)


Total Accepted: 106573 Total Submissions: 784972 Difficulty: Easy

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 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++ function had been updated. If you still see your function signature accepts a const char *

argument, click the Reload button to reset your code definition.

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

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

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 returned.

Subscribe to see which companies asked this question

Hide TagsMath StringHide Similar Problems(E) Reverse Integer (H) Valid number

































Ideas:

Handle the following four elements:

1. Empty string, "0" string processing

2. Remove the front space

3. Judging symbols

4. Character conversion and overflow handling


Java code:

public class Solution {public int myatoi (String str) {int ans = 0;        int sign = 1;        int index = 0;        1. Empty string, "0" string processing if (Str==null | | str.length () ==0) return 0;                int len = Str.length ();                2. Remove the front space while (Str.charat (index) = = ' && index < len) index++;  3. Judgment symbol if (Str.charat (index) = = '-' | | | str.charat (index) = = ' + ') {sign = Str.charat (index) = = ' + '? 1            :-1;        index++;            }//4. Character conversion and overflow processing while (Index < len) {int digit = Str.charat (index)-' 0 ';            if (Digit < 0 | | digit > 9) break; Overflow while (Integer.max_value/10 < ans | | (INTEGER.MAX_VALUE/10 = = ans && integer.max_value%10 < digit)) {return sign = = 1?            Integer.MAX_VALUE:Integer.MIN_VALUE;            } ans = ten * ans + digit;        index++;     }   return sign * ans; }}


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