• LeetCode 8


    一、问题描述

    Description:

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

    实现atoi函数,将一个字符串转成整数。


    二、解题报告

    直接使用字符串流stringstream

    class Solution {
    public:
        int myAtoi(string str) {
            int a;
            stringstream ss;
            ss << str;
            ss >> a;
            return a;
        }
    };

    当然,这太取巧了!本题的意图并不在此。(^__^)

    实现atoi的过程中主要就是要注意异常输入的处理,大致有以下几点:

    1. 忽略、跳过字符串前面的空字符。
    2. 第一个字符可以是'+''-'表示数的正负,也可以没有。
    3. 若第一个字符 或 正负号后第一个字符是非数字字符,直接返回0。
    4. 若字符串中间出现非数字字符,返回该字符前面部分表示的数字。
    5. 若数值超出 int 上界,返回INT_MAX;若超出 int 下界,返回INT_MIN

    实现代码如下:

    class Solution {
    public:
        int myAtoi(string str) {
            int i=-1;
            bool positive = true;    // 标记正负
            while(str[++i]==' ') ;   // 跳过空字符
            if(str[i]=='-' || str[i]=='+') {
                positive = str[i]=='-'?false:true;
                ++i;
            }
    
            if(!isdigit(str[i]))     // 若第一个字符是非数字字符
                return 0;
    
            uint64_t sum = 0;
            uint64_t m10 = 1;
            for(int j=str.size()-1; j>=i; --j)
            {
                if(!isdigit(str[j]))  // 若中间出现非数字字符
                {
                    sum = 0;
                    m10 = 1;
                    continue;
                }
                sum += (str[j]-'0') * m10;
                m10 *= 10;
                if(sum > INT_MAX)
                    break;
            }
    
            if(sum > INT_MAX && positive)
                return INT_MAX;
            else if(sum > INT_MAX && !positive)
                return INT_MIN;
            else
                return positive?sum:-sum;
        }
    };





    LeetCode答案源代码:https://github.com/SongLee24/LeetCode


  • 相关阅读:
    akka并发编程练习
    使用selenium和chromedriver组合爬虫时,如果爬取的页面数量巨多,会出现占用内存逐渐增大知道程序崩溃的情况
    网易2017秋招编程题集合_以下代码全部来自牛客网
    牛客网_运行问题
    json和xml之间转换的好文
    Eclipse 启动时闪退问题解决方案
    关于opencv的文件配置详细内容
    第一个opencv声称图片_以及遇到的问题集锦
    好文收藏_关于find_first_not_of函数
    好文收藏readtxt_cpp
  • 原文地址:https://www.cnblogs.com/songlee/p/5738061.html
Copyright © 2020-2023  润新知