我现在在做一个叫《leetbook》的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看
书的地址:https://hk029.gitbooks.io/leetbook/

008. String to Integer (atoi) [E]


题目

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. while(str[i] == " ") i++;

正负号的处理:我觉得yuruofeifei这个解决方案简直赞

  1. if (str[i] == '-' || str[i] == '+') {
  2. sign = 1 - 2 * (str[i++] == '-');
  3. }
  4. ……
  5. return base * sign;

溢出处理(可以参考上一道题):

  1. if (base > INT_MAX / 10 || (base == INT_MAX / 10 && str[i] - '0' > INT_MAX%10)) {
  2. if (sign == 1) return INT_MAX;
  3. else return INT_MIN;
  4. }

非法输入:其实只用过滤就行了

  1. while (str[i] >= '0' && str[i] <= '9') {
  2. ……
  3. }

代码

我的代码,不够简洁,可以参考yuruofeifei的代码,在下面

  1. class Solution {
  2. public:
  3. int myAtoi(string str) {
  4. long tmp=0;
  5. bool neg;
  6. int i = 0;
  7. while(str[i] == ' ') i++; //读掉空格
  8. neg = str[i] == '-'?1:0;
  9. for(i = i+ (neg || str[i] == '+');i < str.length();i++) //如果是- 或 + i+1跳过符号
  10. {
  11. if(str[i] - '0' >= 0 && str[i] - '0' < 10) //过滤非法输入
  12. {
  13. tmp *= 10;
  14. tmp += (str[i] - '0');
  15. if(tmp >= INT_MAX && !neg) //溢出判断
  16. {
  17. tmp = INT_MAX;
  18. break;
  19. }
  20. if(tmp -1 >= INT_MAX && neg) //除了符号,INT_MAX和INT_MIN只差1
  21. {
  22. tmp = INT_MIN;
  23. break;
  24. }
  25. }
  26. else break;
  27. }
  28. if(neg) return -tmp;
  29. return tmp;
  30. }
  31. };

yuruofeifei的代码

  1. int myAtoi(string str) {
  2. int sign = 1, base = 0, i = 0;
  3. while (str[i] == ' ') { i++; }
  4. if (str[i] == '-' || str[i] == '+') {
  5. sign = 1 - 2 * (str[i++] == '-');
  6. }
  7. while (str[i] >= '0' && str[i] <= '9') {
  8. if (base > INT_MAX / 10 || (base == INT_MAX / 10 && str[i] - '0' > 7)) {
  9. if (sign == 1) return INT_MAX;
  10. else return INT_MIN;
  11. }
  12. base = 10 * base + (str[i++] - '0');
  13. }
  14. return base * sign;
  15. }

《LeetBook》leetcode题解(8): String to Integer (atoi) [E]——正负号处理的更多相关文章

  1. LeetCode题解 #8 String to Integer (atoi)

    又是一道恶心的简单题. 一开始没想到这么多情况的,幸好LeetCode是个很人性化的oj,能让你知道你在哪个case上错了,否则一辈子都过不了. 考虑不周到只能一个个补了. 列举一下恶心的case / ...

  2. 【LeetCode】8. String to Integer (atoi) 字符串转换整数

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:字符串转整数,atoi,题解,Leetcode, 力扣,P ...

  3. 【LeetCode】008. String to Integer (atoi)

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

  4. LeetCode(8) - String to Integer (atoi)

    虽然是easy,却是比较繁琐的一道题,需要考虑各种边界条件.在WA了好几遍之后,才把各种边界条件给补全.需要考虑到的因素如下: 输入不合法字符,非"0-9",对于首位,合法字符还包 ...

  5. 【一天一道LeetCode】#8. String to Integer (atoi)

    一天一道LeetCode系列 (一)题目 Implement atoi to convert a string to an integer. Hint: Carefully consider all ...

  6. 【LeetCode】8. String to Integer (atoi) 字符串转整数

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

  7. 【leetcode】8. String to Integer (atoi)

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

  8. leetcode day6 -- String to Integer (atoi) &amp;&amp; Best Time to Buy and Sell Stock I II III

    1.  String to Integer (atoi) Implement atoi to convert a string to an integer. Hint: Carefully con ...

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

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

随机推荐

  1. php autoload 笔记

    php auotload 实现了类的延迟加载机制,需要的时候在include,平时很少用到.它的实现原理搜了一下如下(不是本人研究的结果): 检查执行器全局变量函数指针autoload_func是否为 ...

  2. Android-bindService本地服务-初步

    在Android开发过程中,Android API 已经有了startService方式,为什么还需要bindService呢? 答:是因为bindService可以实现Activity-->S ...

  3. test4 结对项目

    [必做 1] 基于作业3的结果,读取一个较小的文本文件A_Tale_of_Two_Cities.txt,统计该文件中的单词的频率,并将统计结果输出到当前目录下的 Result1.txt 文件. 结对对 ...

  4. Github注册及心得

    注册Github流程: 1.搜索www.github.com 2.有两个按钮sign up(注册).sign in(登入)

  5. 利用ROW_NUMBER中的partition by 删除重复Key的数据

    With temp As ( Select ROW_NUMBER() over(partition by LogisticsPlan order by createon) rowID,ID from ...

  6. freemarker获取变量的范围的问题

    今天做freemarker的时候,想用一下全局的变量.就是在a.ftl 和 b.ftl页面里面,使用a.action里面放入request的变量.a.action的视图页面是a.ftl ,b.ftl是 ...

  7. solr特点五: MoreLikeThis(查找相似页面)

    在 Google 上尝试一个查询,您会注意到每一个结果都包含一个 “相似页面” 链接,单击该链接,就会发布另一个搜索请求,查找出与起初结果类似的文档.Solr 使用MoreLikeThisCompon ...

  8. asp.net mvc5 下载文件方法

    控制器自带的 FileContentResult 可以让我们很方便的返回文件到服务端,减少了很多步骤.用于下载文件的时候,像视频.文本.图片这种浏览器支持的文件,默认就会被浏览器打开.这时候想让它变成 ...

  9. asp.net 添加错误日志

    在开发程序中,错误日志很有必要.今天就把使用到的添加错误日志,记录下来,方便以后查看 利用的asp.net错误处理机制 Application_Error 贴出代码 protected void Ap ...

  10. MongoDB账号管理及实践

    此文已由作者温正湖授权网易云社区发布. 欢迎访问网易云社区,了解更多网易技术产品运营经验. 目前蜂巢(云计算基础服务)MongoDB上已经有数十个实例,其中不少是企业用户或公司内部产品用户的.用户多了 ...