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;
        }
    };
  • 相关阅读:
    Serializable读写类操作
    socket模拟通信
    使用poi实现生成excel文件
    注解形式的通知
    使用linkedhashmap实现LRU(最近最少使用缓存算法)
    websocket的使用
    centos systemctl daemon-reload 提示 no such file or directory 的一个原因
    mac 10.13 build 一个 redis desktop manager
    mac 必备工具
    supervisor 添加新配置不生效的问题
  • 原文地址:https://www.cnblogs.com/zengzy/p/5016784.html
Copyright © 2011-2022 走看看