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 };
  • 相关阅读:
    解决SharePoint 文档库itemadded eventhandler导致的上传完成后,编辑页面保持报错的问题,错误信息为“该文档已经被编辑过 the file has been modified by...”
    解决SharePoint 2013 designer workflow 在发布的报错“负载平衡没有设置”The workflow files were saved but cannot be run.
    随机实例,随机值
    Spring4笔记
    struts2笔记(3)
    struts2笔记(2)
    获取文本的编码类型(from logparse)
    FileUtil(from logparser)
    DateUtil(SimpleDateFormat)
    struts2笔记
  • 原文地址:https://www.cnblogs.com/anghostcici/p/6626241.html
Copyright © 2011-2022 走看看