博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
String to Integer (atoi) ???
阅读量:4354 次
发布时间:2019-06-07

本文共 4243 字,大约阅读时间需要 14 分钟。

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
'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 else74 {75 //return 0;//如果字符串不能转换为int的话,返回什么呀,这个很纠结呀,提交的错误用例说明返回值应该是076 break;77 }78 }79 if(flag)80 val=-val;81 //Input: "2147483648" Expected: 214748364782 // 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 else88 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: 

Input: " 10522545459"
Output: 1932610867
Expected: 2147483647

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

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!='\0')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             else27             {28                 break;29             }30         }31         while(*str!='\0')32         {33             if(*str<='9'&&*str>='0')34             {35                 digit=*str-'0';36                 //要满INT_MIN<=val*10+digit<=INT_MAX37                 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                 else43                 {44                     value=value*10+digit;45                     str++;46                 }47             }48             else49             {50                 break;51             }52         }53         if(flag)54             value=-value;55         return value;56     }57 };

Submission Result: 

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

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

转载于:https://www.cnblogs.com/crane-practice/p/3595611.html

你可能感兴趣的文章
实验2
查看>>
sublime text 3安装Anaconda插件之后写python出现白框
查看>>
windows服务器详细安全设置
查看>>
如何去把内容分享到whatsapp上?
查看>>
mysql SQL语法总结
查看>>
STM32学习笔记3(TIM通用模块输入捕获)
查看>>
Java线程中,Blocked,Wait,以及TIMED_WAIT的区别
查看>>
python爬虫:登录百度账户,并上传文件到百度云盘
查看>>
彼岸芳华
查看>>
C/C++ 宏技巧
查看>>
CentOS更新源
查看>>
C++11 assert/static_assert
查看>>
第二次结对编程作业
查看>>
python redis使用
查看>>
JavaScript自学笔记 第2次
查看>>
吴恩达机器学习笔记2-监督学习
查看>>
POJ 1734 Sightseeing trip (floyd 求最小环)
查看>>
Android系统手机邮件收发设置教程
查看>>
Python+ITchart实现微信机器人对指定的朋友和群自动回复
查看>>
springboot的注解详解
查看>>