[LeetCode] 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

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.

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) orINT_MIN (-2147483648) is returned.

  • 实现代码
/*************************************************************
    *  @Author   : 楚兴
    *  @Date     : 2015/2/7 14:47
    *  @Status   : Accepted
    *  @Runtime  : 15 ms
*************************************************************/
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
class Solution {
public:
    int atoi(const char *str) {
        long long num = 0;
        while (*str == ' ')  //去掉空格
        {
            str++;
        }

        bool flag = false;  //获取可选的正负号
        if (*str == '+')
        {
            str++;
        }
        else if (*str == '-')
        {
            flag = true;
            str++;
        }

        //如果第一个序列的非空格字符不是有效地数字字符,则返回0
        if (*str < '0' || *str > '9')
        {
            return 0;
        }

        while(*str)
        {
            if (*str >= '0' && *str <= '9')
            {
                num = num * 10 + *str - '0';
                str++;
                if (num > INT_MAX)
                {
                    if (!flag)
                    {
                        return INT_MAX;  //INT_MAX = 2147483647
                    }
                    else
                    {
                        return INT_MIN;  //INT_MIN = -2147483648
                    }
                }
            }
            else
            {
                break;
            }
        }

        if (flag)  //加上负号
        {
            num = -num;
        }

        return num;
    }
};

应该特别注意越界的情况,输入数据甚至可能越long long类型的界。

目录
相关文章
|
10月前
|
算法 C++
【LeetCode】【C++】string OJ必刷题
【LeetCode】【C++】string OJ必刷题
49 0
|
2月前
|
算法
LeetCode第8题字符串转换整数 (atoi)
该文章介绍了 LeetCode 第 8 题字符串转换整数 (atoi)的解法,需要对字符串进行格式解析与校验,去除前导空格和处理正负号,通过从高位到低位的计算方式将字符串转换为整数,并处理越界情况。同时总结了这几道题都需要对数字的表示有理解。
LeetCode第8题字符串转换整数 (atoi)
|
4月前
|
SQL 算法 数据可视化
LeetCode第八题:字符串转换整数 (atoi)【8/1000 python】
LeetCode第八题:字符串转换整数 (atoi)【8/1000 python】
|
5月前
|
机器学习/深度学习 canal NoSQL
从C语言到C++_12(string相关OJ题)(leetcode力扣)
从C语言到C++_12(string相关OJ题)(leetcode力扣)
46 0
|
5月前
|
存储 编译器 Linux
标准库中的string类(中)+仅仅反转字母+字符串中的第一个唯一字符+字符串相加——“C++”“Leetcode每日一题”
标准库中的string类(中)+仅仅反转字母+字符串中的第一个唯一字符+字符串相加——“C++”“Leetcode每日一题”
|
5月前
|
存储 算法 C#
Leetcode算法系列| 8. 字符串转换整数 (atoi)
Leetcode算法系列| 8. 字符串转换整数 (atoi)
|
11月前
|
Java
Leetcode 467. Unique Substrings in Wraparound String
大概翻译下题意,有个无限长的字符串s,是由无数个「abcdefghijklmnopqrstuvwxy」组成的。现在给你一个字符串p,求多少个p的非重复子串在s中出现了?
43 0
|
算法 C++ Python
【力扣算法11】之 8. 字符串转换整数 (atoi) python
【力扣算法11】之 8. 字符串转换整数 (atoi) python
114 0
|
存储 算法 测试技术
力扣7-整数反转&力扣8-字符串转换整数 (atoi)
力扣7-整数反转&力扣8-字符串转换整数 (atoi)
77 0
|
算法 安全 Swift
LeetCode - #8 字符串转换整数(atoi)
不积跬步,无以至千里;不积小流,无以成江海,Swift社区 伴你前行。如果大家有建议和意见欢迎在文末留言,我们会尽力满足大家的需求。