LeetCode 8String to Integer (atoi)
来源:互联网 发布:基于python.的毕业设计 编辑:程序博客网 时间:2024/05/21 15:57
描述
给出一个字符串,把它转换成整数
解决
坑点好多啊,看了题解才A掉的。判断是否有空格,判断字符的符号,还要考虑溢出,还有中间出现非数字字符要中断。
class Solution {public: int myAtoi(string str) { int lenth = str.size(); long long res = 0; int i = 0; while (str[i] == ' ' && i < lenth)++i; int flag = 1; if (str[i] == '-') { flag = -1; ++i; } else if (str[i] == '+') { flag = 1; ++i; } //cout << str[i] << endl; while (i < lenth){ //cout << str[i] << endl; if (!isdigit(str[i])) break; res = res * 10 + str[i] - '0'; if (res * flag < 0 && res * flag< ((int)(1) << 31)) return (int)(1) << 31; if ( res * flag> 0 && res * flag > ((unsigned int)(-1) >> 1)) return ((unsigned int)(-1) >> 1); ++i; } return res * flag; }};
0 0
- LeetCode 8 - String to Integer (atoi)
- [LeetCode 8] String to Integer (atoi)
- leetcode 8STRING TO INTEGER (ATOI)
- Leetcode【8】:String to Integer(atoi)
- [leetcode 8] String to Integer (atoi)
- LeetCode(8) String To Integer(atoi)
- [leetcode 8] String to Integer (atoi)
- LeetCode 8:《String to Integer (atoi) 》
- leetcode.8---------------String to Integer (atoi)
- leetCode #8 String to Integer (atoi)
- [LeetCode Java] 8 String to Integer (atoi)
- leetcode 8 String to Integer (atoi)
- leetcode-8 String to Integer(atoi)
- LeetCode 8 String to Integer (atoi)
- LeetCode(8)String to Integer (atoi)
- LeetCode 8 - String to Integer (atoi)
- leetcode #8 String to Integer (atoi)
- String to Integer (atoi) - LeetCode 8
- Leetcode 7. Reverse Integer
- c语言中整数赋值和字符赋值的一些问题
- 颜色代码大全 - RGB颜色查询对照表
- M1非接触式IC卡性能简介
- spring3,struts2,hibernate,junit,设计模式视频教程下载
- LeetCode 8String to Integer (atoi)
- 喧闹过后,谁是王者?餐饮O2O的四种模式大盘点
- 自动化邮件报告平台-邮件发送highchart图表
- 从 iOS 应用扩展谈起
- 云服务器安装LNMP
- SDUT OJ 2140 图结构练习——判断给定图是否存在合法拓扑序列
- FFMPEG学习----分离视频里的H.264与YUV数据
- php header的用法
- angularJs指令的3种绑定策略