LeetCode [8]. String to Integer (atoi), leetcodeatoi

Source: Internet
Author: User

LeetCode [8]. String to Integer (atoi), leetcodeatoi

String to Integer (atoi)

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.

Update ):
The signature ofC++Function had been updated. If you still see your function signature acceptsconst char *Argument, please click the reload button to reset your code definition.

This article simply converts the numbers in the string into an integer, but the question has the following requirements: 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.

The general meaning is:

1. First, discard the blank characters before the string to know that the first non-blank character is scanned. Then, replace the string with a valid value and the plus or minus sign before it into an int;

2. This legal value sub-string or may contain other non-numeric special characters, ignoring these sub-strings;

3. If the first non-empty sub-string is an unprotected integer value, or does not exist at all, or even the string may be a special or empty character, the conversion is not 0;

4. If there is no valid conversion, 0 is returned. If any return value exceeds the boundary, the boundary value is returned;

I. Train of Thought: although this question is relatively simple, there will always be results of missing details without considering all possibilities, as a result, there will always be new exceptions after submission. For example, "+-15651" and "* 6644" strings that appear for the first time cannot constitute numbers are illegal and 0 is returned directly. The idea is as follows: first scan the first non-null character, and then consider two cases. The first one is that the first non-null character is '+' or, it is not legal to determine whether the next character is a number. The first non-null character is a number, so you can continue to convert it by scanning the number. Except for the preceding two cases, the other cases are invalid.
Ii. Java program

Public class Solution {public int myAtoi (String str) {int sl = str. length (); int index = 0; char tempc; // obtain the boolean flag = false; // negative number marker int firstI =-1; long re = 0; if (sl <= 0) return 0; // 1. start with the first non-empty character while (str. charAt (index) = '') {index ++; if (index> = sl) // exceeds the string length return 0;} // 2. scan the first non-empty character to determine whether it is valid tempc = str. charAt (index); // if the value is +-or a value of 2.1, otherwise, 0 if (tempc = '-' | tempc = '+' | (tempc <= '9' & tempc> = '0 ') {// 2.2 if the value is greater than (tempc <= '9' & tempc> = '0 ')) {} else // if the value is +-2.3, then judge whether the subsequent value is {if (tempc = '-') flag = true; // 2.3.1 if it is A-number, if the flag is set to index ++; // if the value of 2.4 is a +-sign, the system checks whether the next digit is a value. If the value is a valid value, the system returns 0 tempc = str. charAt (index); if (tempc <= '9' & tempc> = '0') {} else {return 0 ;}}} if else // 2.1 is +-or a value, otherwise {return 0;} // 3. start scanning valid numbers while (index <sl) {tempc = str. charAt (index); if (tempc <= '9' & tempc> = '0') {re = re * 10 + (Str. charAt (index)-'0'); if (re> = Integer. MAX_VALUE | re <= Integer. MIN_VALUE) break;} else {break;} index ++;} if (flag = true) {re =-re; re = re <Integer. MIN_VALUE? Integer. MIN_VALUE: re;} else {re = re> Integer. MAX_VALUE? Integer. MAX_VALUE: re;} return (int) re ;}}




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.