题目内容:

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.

Update (2015-02-10):
The signature of the C++ function had been updated. If you still see your function signature accepts a const char * argument, please click the reload button to reset your code definition.

spoilers alert… click to show requirements for atoi.

Requirements for atoi:
The function first discards as many whitespace characters as necessary until the first non-whitespace character is found. Then, starting from this character, takes an optional initial plus or minus sign followed by as many numerical digits as possible, and interprets them as a numerical value.

The string can contain additional characters after those that form the integral number, which are ignored and have no effect on the behavior of this function.

If the first sequence of non-whitespace characters in str is not a valid integral number, or if no such sequence exists because either str is empty or it contains only whitespace characters, no conversion is performed.

If no valid conversion could be performed, a zero value is returned. If the correct value is out of the range of representable values, INT_MAX (2147483647) or INT_MIN (-2147483648) is returned.

解题思路:
题目本身不难,但是有很多异常情况要处理。比如溢出、符号、无效字符串、起始处和结尾处的空格等等。

代码:

class Solution {public:int atoi(string str) {char *result = new char[str.size()+1];int index(0), start(0), rtn(0), sign(0);bool hasDigit(false);while(isspace(str[index]) || str[index]=='0')++index;if(index >= str.size())return rtn;if(str[index] == '-') {++sign;result[start++] = str[index++];}while(str[index]=='0' || str[index]=='+') {if(str[index]=='+')++sign;++index;}while(index != str.size() && isdigit(str[index])) {hasDigit = true;result[start++] = str[index++];}result[start] = '\0';if(result[0] == '\0')return rtn;if(hasDigit && sign < 2)sscanf(result, "%d", &rtn);elsereturn rtn;char *temp = new char[str.size()+1];sprintf(temp, "%d", rtn);if(strcmp(temp, result)) {if(result[0]=='-')return INT_MIN;return INT_MAX;}return rtn;}
};

008 String to Integer (atoi) [Leetcode]相关推荐

  1. String to Integer (atoi) leetcode java

    题目: Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input ca ...

  2. LeetCode算法入门- String to Integer (atoi)-day7

    LeetCode算法入门- String to Integer (atoi)-day7 String to Integer (atoi): Implement atoi which converts ...

  3. 【细节实现题】LeetCode 8. String to Integer (atoi)

    LeetCode 8. String to Integer (atoi) Solution1:我的答案 参考链接:http://www.cnblogs.com/grandyang/p/4125537. ...

  4. Kotlin实现LeetCode算法题之String to Integer (atoi)

    题目String to Integer (atoi)(难度Medium) 大意是找出给定字串开头部分的整型数值,忽略开头的空格,注意符号,对超出Integer的数做取边界值处理. 方案1 1 clas ...

  5. LeetCode 8. String to Integer (atoi)(字符串)

    LeetCode 8. String to Integer (atoi)(字符串) LeetCode 8 String to Integer atoi字符串 问题描述 解题思路 参考代码 By Sca ...

  6. [LeetCode] NO. 8 String to Integer (atoi)

    [题目] Implement atoi to convert a string to an integer. [题目解析] 该题目比较常见,从LeetCode上看代码通过率却只有13.7%,于是编码提 ...

  7. leetcode 8. String to Integer (atoi)

    也许是我没有理解清楚题意,为什么输入+-2的时候要输出0,而不是输出2呢. public class Solution {public int myAtoi(String str) {if(str = ...

  8. 8. String to Integer (atoi)

    题目: Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input ca ...

  9. LeetCodeOJ. String to Integer (atoi)

    试题请參见: https://oj.leetcode.com/problems/string-to-integer-atoi/ 题目概述 Implement atoi to convert a str ...

最新文章

  1. 美国科学家成功恢复老年人工作记忆,望奠定认知干预疗法基础
  2. android游戏加载,Android 游戏引擎libgdx 资源加载进度百分比显示案例分析
  3. Multi Dimension 数据库设计
  4. Struts2+Spring传参
  5. Jack Dongarra/杰克 多加拉
  6. IntelliJ IDEA(一、下载,安装与激活)
  7. vue.js常用命令
  8. 倒果汁c语言,水果榨汁补维生素C?这些补维生素的错误别再犯了
  9. 使用 Spring 简化 MyBatis
  10. 计算机算法刘汉英PDF,操作系统原理(刘汉英)
  11. .net5 不支持winform_昨晚实操一波.NET5,极致性能简直逆天!
  12. js 如何拿到后天的时间_js获取日期及日期相关js方法 积累总结
  13. numpy教程:numpy基本数据类型及多维数组元素存取
  14. Leetcode513. Find Bottom Left Tree Value找树左下角的值
  15. oracle日期按时间点过滤,Oracle数据库日期过滤方法性能比较
  16. EasyUI框架04——treegrid
  17. PortMap端口映射器(提供下载资源)
  18. 企业微信API之通讯录同步
  19. html转化pdf文字无法显示,HTML 转 PDF 排雷 之 wkhtmltopdf 转换得到的PDF没有内容
  20. 百度卫士+7654联盟

热门文章

  1. 广州外贸企业邮箱哪个好?公司企业邮箱微信oa办公系统
  2. 黄东旭: 关于基础软件产品价值的思考
  3. Unity3d将vs关联起来/unity3d设置默认脚本编辑器
  4. java 屏蔽广告js_js屏蔽广告
  5. 第5模块闯关CSS练习题
  6. 阿里云天池大赛工业蒸汽预测学习(3)
  7. 香港大学计算机硕士一定要面试吗,大干货!!!香港硕士有哪些专业需要笔试面试...
  8. java 操作pdf_java操作PDF(PDFBOX和Itext框架)
  9. 全国计算机等级一级photoshop证书,全国计算机等级考试一级Photoshop考试大纲
  10. lisp代码编写地物符号_工程图中标注序号的LISP程序