LeetCode 008 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.

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, starting from this character, takes an optional initial
plus or minus sign followed by as many numerical digits as possible, 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 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 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.

【题意】

题意实现atoi库函数,将字符串数组转化为整数

【思路】

需要考虑以下几种情况

1. 字符串为空, gui

2. 整数可正可负,其中"+"可有可无,

+1,+0,-0也是合法的

3. 整数越界情况如何处理,溢出下边界则返回INT_MIN, 溢出上边界则返回INT_MAX

4. 数字中间可能出现空格 "1 2"=>12

5. 字符串只能由空格 + - 数字四种字符组成,且

(1)+ - 最多出现一次,且只能出现在所有数字之前

(2)数字至少出现一次

看了下题目的提示,比上面考虑的要简单的多

1. 过滤掉起始的左右空格,遇到+-数据后开始向后取最长的连续数字序列,转换成整数即可

" 11"->11

"  +12abc"->12

2. 如果没有这样的数字序列,则不作处理。但这种情况返回什么值呢?题目没说。这里就默认返回0吧

【代码】

class Solution {
public:

    int atoi(const char *str) {
        long long result=0;
        int sign=0;   //标记正负符号 -1负号,1正号,0还没有确定正负
        const char*p=str;
        //找到第一个+-或者数字
        while(*p!=‘\0‘ && *p==‘ ‘)p++;
        if(*p!=‘\0‘){
            if(*p==‘+‘){sign=1; p++;}
            else if(*p==‘-‘){sign=-1; p++;}
            else if(*p>=‘0‘&&*p<=‘9‘){result=*p-‘0‘; sign=1; p++;}
            else return result;
        }
        //扫描后续的的连续数字序列
        while(*p!=‘\0‘&&*p>=‘0‘&&*p<=‘9‘){
            result=result*10+(*p-‘0‘);
            p++;
        }
        //返回结果
        result*=sign;
        if(result>INT_MAX)result=INT_MAX;
        else if(result<INT_MIN)result=INT_MIN;
        return (int)result;
    }
};

LeetCode 008 String to Integer (atoi),布布扣,bubuko.com

时间: 2024-10-27 00:09:40

LeetCode 008 String to Integer (atoi)的相关文章

【LeetCode】008 String to Integer (atoi)

题目:LeetCode 008 String to Integer 题意:完成内置函数atoi的功能,将字符串转换成整数. 教训:一开始理所应当的随便一写,然后发现有很多的异常情况需要处理.然后按照C++ Reference中关于atoi的规定一条一条写,才AC.另外还有一个溢出的问题,一开始以为int会自动处理直接返回边界值,其实不是,如果溢出的话大于2147483647的数会给变成负数,因此要单独判断是否会大,但是设置成longlong 之后出现的问题是,还有可能会溢出longlong,所以

LeetCode:String to Integer (atoi)

1.题目名称 String to Integer (atoi) (字符串到数字的转换) 2.题目地址 https://leetcode.com/problems/string-to-integer-atoi/ 3.题目内容 英文:Implement atoi to convert a string to an integer. 中文:实现atoi函数,将输入的字符串(String类型)转换为整型数据(Integer类型) 提示:实现的atoi函数需要满足以下特征 忽略字符串第一个非空格字符前的所

Leetcode 数 String to Integer (atoi)

本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie String to Integer (atoi) Total Accepted: 9862 Total Submissions: 67880 Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. If you want a challenge,

leetcode day6 -- String to Integer (atoi) &amp;&amp; Best Time to Buy and Sell Stock I II III

1.  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 inte

【leetcode】String to Integer (atoi)

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 f

[LeetCode][JavaScript]String to Integer (atoi)

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 f

leetCode 8. String to Integer (atoi) 字符串

8. 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 intende

[LeetCode] 008. String to Integer (Easy) (C++/Java/Python)

索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 008.String_to_Integer (Easy) 链接: 题目:https://oj.leetcode.com/problems/string-to-integer-atoi/ 代码(github):https://github.com/illuz/leetcode 题意: 将一个字符串转化为 int 型.

[LeetCode][Algorithms]String to Integer (atoi)

# -*- coding: utf8 -*-'''__author__ = '[email protected]'https://oj.leetcode.com/problems/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