【8】String to Integer (atoi)
来源:互联网 发布:网络推广外包收费 编辑:程序博客网 时间:2024/05/22 17:05
此题主要就是注意一些trick
前导空格、非数字字符、溢出等
int myAtoi(string str) { int res=0; int max_inf=0x7fffffff; int min_inf=0x80000000; int p=1; int i=0; while(isspace(str[i])||str[i]=='0')i++; if(str[i]=='-'){p=-1;i++;} else if(str[i]=='+')i++; else if(str[i]<'0'||str[i]>'9')return 0; for(;i<str.length();i++) { int newd=str[i]-'0'; if(str[i]<'0'||str[i]>'9')break; if(p==1&&(res>max_inf/10 || res==max_inf/10&&(newd>max_inf%10)))return max_inf; if(p==-1&&(res>max_inf/10 || res==max_inf/10&&(newd>-(min_inf%10))))return min_inf; res=res*10+newd; } if(p==-1)res=-res; return res; }
0 0
- 8、String to Integer (atoi)
- 8,String to Integer (atoi)
- 8 String to Integer (atoi)
- 8 String to Integer (atoi)
- #8 String to Integer (atoi)
- 8String to Integer (atoi)
- 8 String to Integer (atoi)
- 8 - String to Integer (atoi)
- [8]String to Integer (atoi)
- 8、String to Integer (atoi)
- 【8】String to Integer (atoi)
- 8String to Integer (atoi)
- 8 String to Integer (atoi)
- String to Integer (atoi)
- String to Integer (atoi)
- String to Integer (atoi)
- String to Integer (atoi)
- String to Integer (atoi)
- 简单的计步器实现
- 调用WCF传递JSON格式数据
- UTA使用说明
- poj1511【最短路spfa】
- Java 静态代码块和非静态代码块
- 【8】String to Integer (atoi)
- 回调接口学习笔记
- Android layer-list:联合shape(2)
- eclipse自配模板
- ionic http请求设置全局动画
- mysql locate函数
- 【Spring】queryForXXX()方法使用的一些细节问题
- 扩展KMP
- hdu 2888 二维线段树和二维ST模版