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 };
  • 相关阅读:
    Android为TV端助力 转载:RecyclerView分页加载
    Android 最简单的测试UI卡顿
    Android为TV端助力 使用shared注意事项
    Android为TV端助力 电影栏目移动到底部或者顶部时抖动动画
    Android为TV端助力转载:码农小阿飞(SpannableString)
    Android为TV端助力 监听APK卸载,替换,完成的广播
    Android 为TV端助力
    Android为TV端助力 进制互相转换
    Android为TV端助力linux命令
    Android为TV端助力 集成第三方播放器,实现全屏播放
  • 原文地址:https://www.cnblogs.com/anghostcici/p/6626241.html
Copyright © 2011-2022 走看看