zoukankan      html  css  js  c++  java
  • 8. 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.

     
    steps:
    1. find the first non-whitespace character
    2. starting from this character, takes an optional inital plus or minus sign
    3. if the next character is a number, convert to int; if not, return the current result value.
    4. consider the overflow. (line 5&16)
     
     1 class Solution {
     2 public:
     3     int myAtoi(string str) {
     4         int i=0;
     5         long long res=0;
     6         int size=str.length();
     7         int sign=1;
     8         if (size==0) return 0; //if string is empty,return 0
     9         while(str[i]==' ') i++; //find the first non-whitespace character
    10         if (str[i]=='+'||str[i]=='-') {//if have the optional sign
    11             sign=(str[i++]=='+')?1:-1;
    12         }
    13         for (;i<size;i++){ 
    14             if (str[i]<='9' && str[i]>='0'){
    15                 res=res*10+str[i]-'0';
    16                 if (res*sign>INT_MAX || res*sign<INT_MIN) return (sign==1)?INT_MAX:INT_MIN;
    17             }
    18             else break;
    19         }
    20         res=sign*res;
    21         return res;
    22     }
    23 };
  • 相关阅读:
    软件工程第十四周学习进度条
    软件工程第十五周学习进度条
    课堂练习-买书价格最低
    找水王2
    Struts结合马士兵视频的学习经验
    Spring结合马士兵视频的学习经验
    浅谈 《大型网站技术架构》 五六七章
    以《淘宝网》为例,描绘质量属性的六个常见属性场景
    浅谈架构漫谈
    软件架构设计师工作流程
  • 原文地址:https://www.cnblogs.com/anghostcici/p/6626241.html
Copyright © 2011-2022 走看看