zoukankan      html  css  js  c++  java
  • String to Integer (atoi) ???

     1 #define INT_MAX 2147483647
     2 #define INT_MIN -2147483648
     3 class Solution {
     4 public:
     5     int atoi(const char *str) {
     6         //string如果能转换成int的话,各位都要是数字,一个例外是第一个字符可以是负号
     7         int n=strlen(str);
     8         int i=0;
     9         int flag=0;//0表示正值,1表示负值
    10         int val=0;
    11         /*
    12         if(str[0]=='-')
    13         {
    14             i=1;
    15             flag=1;
    16         }
    17         else if(str[0]=='+')//1、好吧,我承认刚开始我只记得负号要单独判断,忘记了正号了,~~~~(>_<)~~~~ 
    18         {
    19             i=1;
    20         }
    21         //2、好吧,我服了,还有空格,我也都没有考虑,空格可能出现的位置:开头,中间,结尾
    22         //3、我彻底服了,空格在开头和结尾还不能够一视同仁呀,因为"    -123"~~~~(>_<)~~~~
    23         */
    24         //这个是专门处理开头空格滴
    25         /*
    26         while(i<n)
    27         {
    28             if(str[i]==' ')
    29                 ++i;
    30             else if(str[i]=='-')
    31             {
    32                 ++i;
    33                 flag=1;
    34                 break;
    35             }
    36             else if(str[i]=='+')
    37             {
    38                 ++i;
    39                 break;
    40             }
    41         }
    42         *///一直显示超时,调试才发现这里是个死循环,晕
    43         while(i<n)
    44         {
    45             if(str[i]==' ')
    46                 ++i;
    47             else if(str[i]=='-')
    48             {
    49                 ++i;
    50                 flag=1;
    51                 break;
    52             }
    53             else if(str[i]=='+')
    54             {
    55                 ++i;
    56                 break;
    57             }
    58             else
    59                 break;
    60         }
    61         while(i<n)
    62         {
    63             //if(str[i]==' ')     //其实字符串中间有空格的话,算是能转为int呀还是不能转为int呀,纠结,我的写法是算是能转了
    64                 //++i;//如果用for时,因为后面已经++i了,所以这里应该是空操作,不能再写++i了
    65                 //Input:"  -0012a42" Output:0  Expected:-12  测试用例的意思是,把能返回的第一串都给返回了
    66             //if(str[i]<'9'&&str[i]>'0')    //写错了吧
    67             if(str[i]<='9'&&str[i]>='0')
    68             {
    69                 //val=val*10+str[i];
    70                 val=val*10+(str[i]-'0');
    71                 ++i;//这里用的是while,所以这里要手动++i;
    72             }
    73             else
    74             {
    75                 //return 0;//如果字符串不能转换为int的话,返回什么呀,这个很纠结呀,提交的错误用例说明返回值应该是0
    76                 break;
    77             }
    78         }
    79         if(flag)
    80             val=-val;
    81         //Input:    "2147483648"  Expected:    2147483647
    82         // If the correct value is out of the range of representable values, INT_MAX (2147483647) or INT_MIN (-2147483648) is returned.
    83         if(flag==0&&val<0)//判断是否溢出
    84             return INT_MAX;
    85         else if(flag==1&&val>0)
    86             return INT_MIN;
    87         else
    88             return val;
    89     }
    90 };

    刚开始我没有看提示,直接做的,然后就各种错呀

    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.  

    Submission Result: Wrong Answer

    Input: " 10522545459"
    Output: 1932610867
    Expected: 2147483647

    说明我的溢出判断那个地方写的不对,对于这道题目来说,怎么判断溢出,什么时候判断溢出,我觉得如果在最后判断溢出是不好的,因为输入的是字符串,不能知道它的int值是多少就不能和之后出来的值比较,所以判断溢出应该是在转换的过程中判断的,至于具体怎么做,我想不到,看看别人是怎么写的吧:http://blog.csdn.net/ithomer/article/details/8800530

     1 class Solution {
     2 public:
     3     int atoi(const char *str) {
     4         int i=0;
     5         int n=strlen(str);
     6         int flag=0;
     7         int value=0;
     8         int digit=0;
     9         while(*str!='')
    10         {
    11             if(*str==' ')
    12             {
    13                 str++;
    14             }
    15             else if(*str=='-')
    16             {
    17                 flag=1;
    18                 str++;
    19                 break;
    20             }
    21             else if(*str=='+')
    22             {
    23                 str++;
    24                 break;
    25             }
    26             else
    27             {
    28                 break;
    29             }
    30         }
    31         while(*str!='')
    32         {
    33             if(*str<='9'&&*str>='0')
    34             {
    35                 digit=*str-'0';
    36                 //要满INT_MIN<=val*10+digit<=INT_MAX
    37                 if(flag==0&&value>(INT_MAX-digit)/10)
    38                     return INT_MAX;
    39                 //else if(value<(INT_MIN-digit)/10)//负值不能这么算
    40                 else if(flag==1&&value>(-INT_MIN-digit)/10)
    41                     return INT_MIN;
    42                 else
    43                 {
    44                     value=value*10+digit;
    45                     str++;
    46                 }
    47             }
    48             else
    49             {
    50                 break;
    51             }
    52         }
    53         if(flag)
    54             value=-value;
    55         return value;
    56     }
    57 };

    Submission Result: Wrong Answer

    Input: " -00134"
    Output: -2147483648
    Expected: -134

    不明真相,我在codeblocks中跑了代码,输入" -00134",输出的就是-134,不知道到底是怎么回事

  • 相关阅读:
    前端常见跨域解决方案(全)
    小程序动画wx.createAnimation
    判断对象是否有某个属性
    占位符
    vue起航——搭建脚手架
    微信小程序-携带参数转发分享页面
    JS获取地址栏参数
    微信小程序wx:if vs hidden
    小程序websocket(心跳连接)
    初识websocket
  • 原文地址:https://www.cnblogs.com/crane-practice/p/3595611.html
Copyright © 2011-2022 走看看