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 };
  • 相关阅读:
    插入排序
    python -- 给电视剧重命名
    程序员你为什么迷茫?
    如何把自己打造成技术圈的 papi 酱
    GitHub中国区前100名到底是什么样的人?
    Python+opencv 图像拼接
    VS2015 新Web项目(C#6)出现CS1617错误的解决
    .Net Task<T>的一种比较神奇的卡死情况(Wait/Result卡死, await能得到结果)
    Xamarin Android自学和实践步骤
    跨过几个坑,终于完成了我的第一个Xamarin Android App!
  • 原文地址:https://www.cnblogs.com/anghostcici/p/6626241.html
Copyright © 2011-2022 走看看