• 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


  • 相关阅读:
    SqlCeConnectionBeginTransaction 方法
    父子继承窗体,子窗体视图无法正常打开,解决办法
    Windows Mobile 如何和模拟器关联有用的URL
    Windows Mobile 6.5.3 Developer Tool Kit
    通过Eclipse import导入项目,並重新命名Project
    【杂】Oracle使用记录:分区表及执行计划
    实践 2-0 selenium使用的一些总结
    实践2-1 python连接Oracle数据库
    【杂】word文件加密和压缩加密
    【杂】HIVE使用记录:回收站及从回收站恢复分区表
  • 原文地址:https://www.cnblogs.com/songlee/p/5738061.html
Copyright © 2020-2023  润新知