leetcode中字符串转化为数字
来源:互联网 发布:mac登录不上apple id 编辑:程序博客网 时间:2024/06/05 05:15
8. 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.
fas
大概就是说,我们在字符串的前面可以加入空格,和数字的符号+-,然后数字中间不能有其它字符,代码如下
int myAtoi(char* str) { if(*str==NULL) return 0; int res=0; int sign=1; int i=0; while(isspace(*str)) str++; if(str[0]=='-'){ sign=-1; i++; } else if(str[0]=='+'){ sign=+1; i++; }else{ sign=+1; } while(str[i]>='0'&&str[i]<='9'){ if(res>INT_MAX/10||(res==INT_MAX/10&&str[i]-'0'>7)){ if(sign==1) return INT_MAX; else return INT_MIN; } res=10*res+(str[i++]-'0'); } return sign*res; }
0 0
- leetcode中字符串转化为数字
- javascript中字符串转化为数字
- java中,String字符串转化为数字
- java中String字符串转化为数字
- 字符串转化为数字
- 字符串转化为数字
- 字符串转化为数字
- Unicode-数字转化为字符串
- 字符串转化为数字函数
- 数字转化为字符串函数
- 字符串转化为数字函数
- 数字转化为字符串函数
- js字符串转化为数字
- 将字符串转化为数字
- 字符串转化为数字 aotf
- 将数字转化为字符串
- Nodejs -- 字符串转化为数字
- 函数:字符串转化为数字
- MySQL常用命令详解
- MyBatis的动态SQL详解
- GIT用法及简介
- Android动画机制与使用技巧
- MATLAB中 diff 函数的用法
- leetcode中字符串转化为数字
- Intent五中启动其它Activity的方式
- 第三方框架的使用
- Kafka源码深度解析-序列7 -Consumer -coordinator协议与heartbeat实现原理
- HDU 1757 矩阵快速幂
- 用Python做投资-小试牛刀
- 丽兹行项目
- ConvertLUnicodeStringToNullTerminatedString
- MPI并行计算与矩阵(2)