string to integer (atoi)
来源:互联网 发布:95年nba总决赛数据 编辑:程序博客网 时间:2024/04/25 12:17
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. starts by ' '
2. followed possible spaces should be '+/-', and no space is allowed later
3. after the single +/-, only 0-9
4. must take care of INT overflow
class Solution {public: int atoi(const char *str) { // Note: The Solution object is instantiated only once and is reused by each test case. if(str==NULL) return 0; int flag=1; long long val=0; const char *p=str; for(int i=0; *p!='\0' ; p++)//i>0 only if a 0-9 appears { if( i==0 && (*p=='+' || *p=='-') )//' 'is only allowed before '+/- { i++; if(*p=='-') flag=-1; continue; } else if(i==0 && *p==' ') continue; if(*p<'0' || *p>'9') { //invalid input, then return the current valid number return (int)val*flag; } else{ val=(long long)10*val+(long long)(*p-'0'); if(val > INT_MAX && flag==1) return INT_MAX; //check whether overflow int. If is, return INT_MAX else if(val > INT_MAX) return INT_MIN; i++; } } return (int)val*flag; }};
- String to Integer (atoi)
- String to Integer (atoi)
- String to Integer (atoi)
- String to Integer (atoi)
- String to Integer (atoi)
- String to Integer (atoi)
- String to Integer (atoi)
- String to Integer (atoi)
- string to integer (atoi)
- String to Integer (atoi)
- String to Integer (atoi)
- String to Integer (atoi)
- String to Integer (atoi)
- String to Integer(atoi)
- String to Integer (atoi)
- String to Integer (atoi)
- String to Integer (atoi)
- String to Integer (atoi)
- 表的连接方式:NESTED LOOP、HASH JOIN、MERGE JOIN(修改)
- C语言实现大小写字母的转换
- mysql 一条命令直接加载主库数据到备库
- Reactor模式及在DSS中的体现
- 053 V10.02 1-100
- string to integer (atoi)
- IO中同步、异步与阻塞、非阻塞的区别
- MTK QT flashtool试用总结
- CD-ROM mode1和mode2
- AndroidMainfest.xml
- 一些不错的中小公司
- SUBMIT的作用和用法
- 看板原理
- java获取字符串格式日期向前或向后n天的日期