Leetcode Note: String to Integer (atoi)

Source: Internet
Author: User

I. Title Description

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.

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 on 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 Returne D.

Two. Topic analysis

The content of this issue is very long, in fact, it mainly describes some possible boundary problems. For integers, the two major problems are the sign and the question of whether the integer range is out of bounds.

The idea is relatively simple, is to first remove the extra space characters, and then read the symbol (note signs are possible, there may be no symbols), followed by sequentially read the number, the end condition has three kinds of conditions:

    1. The exception character appears (according to Atoi function's stipulation is, the exception word character after all truncate, retains the previous part as the result);
    2. Number is out of bounds (returns the nearest integer);
    3. The string ends.

Three. Sample code

As long as attention to the boundary problem, programming is not too difficult, because of limited time, the first reference to other people's program.

//Time complexity O (n), Space complexity O (1) class solution { Public:intAtoi (constChar*Str) {intnum =0;intSign =1; Constintn = strlen (Str);inti =0; while(Str[I] = ="'&& i < n) i++;if(Str[I] = =' + ') i++;Else if(Str[I] = ='-') {sign =-1;        i++; } for(; i < n; i++) {if(Str[I] <' 0 '||Str[I] >' 9 ') Break;if(Num > Int_max/Ten|| (num = = Int_max/Ten&& (Str[I]-' 0 ') > Int_max%Ten))             {returnSign = =-1?            Int_min:int_max; num = num *Ten+Str[I]-' 0 '; }returnnum * SIGN; }};

Four. Summary

This kind of problem often occurs in interview, corresponding to have integer to string. It is the key to consider the boundary condition in this kind of problem.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

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