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.

    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 }
  • 相关阅读:
    php算法之选择排序
    php算法之插入排序
    五分钟读懂UML类图
    ZC_知识点
    JNI_C#
    JNI_Z
    20160419__第1课_第6课
    Hibernate异常_01
    ZC_注意点
    ZC_知识点
  • 原文地址:https://www.cnblogs.com/midhillzhou/p/8780195.html
Copyright © 2011-2022 走看看