atoi函数实现
来源:互联网 发布:淘宝装修代码在线生成 编辑:程序博客网 时间:2024/06/06 02:41
对应leetcode: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.
题解:细节题:注意各种情况
1.不规则输入,但是有效。"-3924"."+20"
2.无效格式,"++c","++1"
3.溢出数据,"2147483648"
class Solution {public: int myAtoi(string str) { int num = 0; int sign = 1; const int n = str.length(); int i = 0; while(str[i]==' '&&i<n) i++; if(str[i]=='+') i++; else if(str[i]=='-'){ sign = -1; i++; } for(;i<n;i++){ if(str[i]<'0'||str[i]>'9') break; if(num>INT_MAX/10|| (num==INT_MAX/10&&(str[i]-'0')>INT_MAX%10)){ return sign == -1?INT_MIN:INT_MAX; } num=num*10+str[i]-'0'; } return num*sign; }};
0 0
- atoi函数实现
- atoi,itoa函数实现
- atoi函数的实现
- atoi()函数的实现
- atoi函数实现
- atoi函数实现
- atoi函数实现
- atoi函数实现_buwanmei
- 实现atoi函数
- atoi函数实现
- atoi()函数的实现
- atoi函数的实现
- atoi函数的实现
- atoi函数实现
- atoi函数实现
- atoi函数实现
- atoi函数的实现
- atoi函数实现
- Linux常用命令
- JS原生之----滚动条
- 关于JDBC中的ResultSetMetaData获取列名字
- FDAC批量导入数据
- 面向对象的三个特征
- atoi函数实现
- 类的继承和构造器重载
- elasticsearch reference 2.3 学习笔记
- Android设计模式系列--工厂方法模式
- 盘点8种CSS实现垂直居中水平居中的绝对定位居中技术
- Android状态栏一体化(改变状态栏的背景颜色)开源工程推荐
- 编译出现提示:ld: 7 duplicate symbols for architecture i386
- HttpClient4.5详解
- Android官方开发文档Training系列课程中文版:电池续航时间优化之检查、检测网络连接状态