• 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.

    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.

    吐槽一下,本题目花了我很长时间用于调试,因为我并不明白,什么样的测试用例应该得到什么样的测试结果。

    思考:首先从字符串里提取出数字字符串,并且赋给新的字符数组,以后就和原先的数组没关系啦,好清爽的感觉。下面就是遍历一下这个新数组,并根据正负数标记,得到最终结果。

     1 int myAtoi(char* str) {
     2   
     3     if(!strlen(str)) return 0;
     4     
     5     int index1 = 0;
     6     int index2;
     7     int negative;
     8     char *p = str;
     9     char *str2;
    10     int i,j;
    11     int result=0;
    12     
    13     //jump to first number.
    14     while(*p!='') {
    15         if(*p=='0' || *p=='1' || *p=='2' || *p=='3' || *p=='4' || *p=='5' || *p=='6' || *p=='7' || *p=='8' || *p=='9') break;
    16         //just for pass test case.
    17         if(*p!=' ' && *p!='+' && *p!='-') return 0;
    18         p++;
    19         index1++;
    20     }
    21     
    22     //can not process string contain no number.
    23     if(*p=='') return 0;
    24     
    25     index2 = index1;
    26     
    27     while(*p!='') {
    28         if(*p!='0' && *p!='1' && *p!='2' && *p!='3' && *p!='4' && *p!='5' && *p!='6' && *p!='7' && *p!='8' && *p!='9') break;
    29         p++;
    30         index2++;
    31     }
    32     //index2 point the last number index.
    33     index2 = index2 - 1;
    34     
    35     str2 = malloc(sizeof(char)*(index2-index1+2));
    36     j = index1;
    37     for(i=0;j<=index2;i++) {
    38         str2[i] = str[j];
    39         j++;
    40     }
    41     str2[i] = '';
    42     
    43     //just for pass test case.
    44     if(index1>=2 && (str[index1-2]=='+' || str[index1-2]=='-')) return 0;
    45     if(index1>=3 && (str[index1-3]=='+' || str[index1-3]=='-')) return 0;
    46     
    47     if(index1!=0 && str[index1-1]=='-')
    48         negative = 1;
    49     else 
    50         negative = 0;
    51     
    52     
    53     if(negative && strlen(str2)>10) return INT_MIN;
    54     if(!negative && strlen(str2)>10) return INT_MAX;
    55         
    56     if(negative && strlen(str2)==10 && strcmp(str2,"2147483648") > 0)
    57         return INT_MIN;
    58     
    59     if(!negative && strlen(str2)==10 && strcmp(str2, "2147483647")>0)
    60         return INT_MAX;
    61    
    62     p = str2;
    63     while(*p!='') {
    64         
    65         result = result*10 + *p-'0';
    66         p++;   
    67     }
    68     
    69     if(negative) 
    70         return -result;
    71     else 
    72         return result;
    73 }
  • 相关阅读:
    太可爱了!CSS3 & SVG 制作的米老鼠钟表
    20个免费的 AngularJS 资源和开发教程
    比尔盖茨:反垄断案让我分心,不然微软定能打败安卓(胜者通吃的行业要不计代价的三班倒,评论很精彩)
    C++11 新特性之智能指针(shared_ptr, unique_ptr, weak_ptr)
    C++编译器会对没有构造函数的类生成默认构造函数吗?(有必要的时候才生成,要看情况。有反汇编验证)
    qt5信息提示框QMessageBox用法(很全)
    (RPC) Remote Procedure Call Protocol 远程过程调用协议
    分布式事务就是由多个本地事务组合而成的事务
    内存管理--虚拟内存管理技术
    NET适合搞大数据,机器学习、人工智能
  • 原文地址:https://www.cnblogs.com/midhillzhou/p/8780195.html
Copyright © 2020-2023  润新知