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.数字前面有空格 如s=“    123456”
2.数字前出现了不必要或多于1个的字符导致数字认证错误,输出0   如s=“   b1234”  ,s=“  ++1233” , s=“ +-1121”
3.数字中出现了不必要的字符,返回字符前的数字 如s=“   12a12” , s=“ 123  123”
4.数字越界 超过了范围(-2147483648--2147483647) 若超过了负数的 输出-2147483648  超过了正数的输出2147483647
在科普一个知识点,倘若某个数超过了2147483647则会变为负数,反过来一样

 class Solution {
public:
int myAtoi(string str) {
long long result = ;
int i = ;
int flag1 = ;
int flag2 = ;
if(str.empty()) return ;
while(str[i] != '\0' && str[i] == ' ') i++;
while(str[i] == '-') { flag1++;i++; }
while(str[i] == '+') { flag2++;i++; }
while(i < str.length())
{
if(str[i] >= '' && str[i] <= '')
{
if(flag1 > ||flag2> || (flag1 == &&flag2 == ))
{
result = ;
break;
}
else if(flag1 == )
{
result = result * - (str[i]-'');
if(result < INT_MIN) result = INT_MIN;
}
else
{
result = result * + (str[i]-'');
if(result > INT_MAX) result = INT_MAX;
}
i++;
}
else
{
break;
}
}
int num = (int)result;
return num;
}
};

最新文章

  1. 无法启动程序 ”*.lib”
  2. The 2015 China Collegiate Programming Contest G. Ancient Go hdu 5546
  3. 安装和使用sass
  4. HTML5 中canvas支持触摸屏的签名面板
  5. HTTP MIME类型即HttpResponse.ContentType属性值列表
  6. vi编辑器基本用法介绍
  7. Python学习 之 文件
  8. css3学习笔记之多列
  9. 前端--关于客户端javascript
  10. Spring MVC 处理异常的3种方式
  11. C语言与java语言中数据类型的差别总结
  12. Python题集:2019春Python程序设计选修课习题笔记
  13. [LeetCode] Random Pick with Blacklist 带黑名单的随机选取
  14. [PHP] PHP闭包(closures)
  15. RMAN restore fails with ORA-01180: can not create datafile 1
  16. hadoop 伪分布式搭建
  17. GIT好文搜藏
  18. 052 kafka对topic的增删改查操作
  19. springboot2.0动态多数据源切换
  20. P1006 传纸条-洛谷luogu-dp动态规划

热门文章

  1. BZOJ1707 : [Usaco2007 Nov]tanning分配防晒霜
  2. AMPPZ2014
  3. BZOJ3682 : Phorni
  4. Codeforces Round #189 (Div. 2) A. Magic Numbers
  5. TYVJ P1088 treat Label:鞭笞人的DP
  6. SqlServer主键外键添加及判断表是否存在
  7. cmd命令行查看windows版本
  8. prelaod场景,用来显示资源加载进度
  9. nodejs安装和环境搭建
  10. OpenCV学习笔记——点击显示鼠标坐标