zoukankan      html  css  js  c++  java
  • 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 (2015-02-10):
    The signature of the C++ function had been updated. If you still see your function signature accepts a const char * argument, please click the reload button  to reset your code definition.

    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.

    class Solution {
    public:
        int myAtoi(string str) {
            /*1.数字表示为:零个或多个空格+可选正负号+全数字+任意字母
              2.如果str为空或者不是1所示的表示形式,认为str为非法表示,不能转化
              3.超出整数所能表示的范围
              4.非法转化返回0,超出表示范围的返回最大正数或最小负数
            */
            int sign = 1;
            int strSize = str.size();
            int i=0;
            for(;i<strSize && str[i]==' ';i++);
            if(i==strSize){//全是空格,非法
                return 0;
            }
            if((str[i]<'0' || str[i]>'9')  &&  (str[i]!='+' && str[i]!='-')){//第一个非空格字符既不是数字也不是正负号,非法
                return 0;
            }
            if(str[i]=='-'){
                sign = -1;
            }
            if(str[i]=='+' || str[i]=='-'){
                i++;
            }
            long long res = 0;
            while(i<strSize){
                if(str[i]<'0' || str[i]>'9'){//非数字,非法
                    return res;
                }
                res = res*10+(str[i++]-'0')*sign;
                if(sign==1 && res>2147483647){//正数
                    return 2147483647;
                }
                if(sign==-1 && res<-2147483648){//负数
                    return -2147483648;
                }
            }
            return res;
        }
    };
  • 相关阅读:
    30天敏捷结果(26): 如何解决问题
    30天敏捷结果(30):提升敏捷结果
    30天敏捷结果(27):做些有重要意义的事
    STM32笔记A
    嵌入式linux驱动开发入门总结
    2014年个人计划
    迄今为止见过最好的职业规划
    30天敏捷结果(28):撰写你的个人使命
    C语言深度解剖读书笔记
    基于 SQLite 数据库的 C# 实体模型的实现
  • 原文地址:https://www.cnblogs.com/zengzy/p/5016784.html
Copyright © 2011-2022 走看看