题目:

参考解法:

I think we only need to handle four cases:

discards all leading whitespaces

sign of the number

overflow

invalid input

int myAtoi(char* str)
{int sign = 1, base = 0, i = 0;while (str[i] == ' ') { i++; }//去掉空格if (str[i] == '-' || str[i] == '+') {sign = 1 - 2 * (str[i++] == '-');//这里i进行了++,注意符号也要输出的 }//判断符号while (str[i] >= '0' && str[i] <= '9') {if (base >  INT_MAX / 10 || ((base == INT_MAX / 10) &&  (str[i] - '0' > 7))) //这个条件会在加到一定程度时才触发{if (sign == 1) return INT_MAX;elsereturn INT_MIN;}base  = 10 * base + (str[i++] - '0');}return base * sign;
}

分析:

注意这里的判断条件:

base == INT_MAX / 10 &&  (str[i] - '0' > 7),最后一个为什么是这个?

LeetCode8——String to Integer (atoi)(自己编写atoi函数)相关推荐

  1. String to Integer (atoi) leetcode java

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

  2. 8. String to Integer (atoi)

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

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

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

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

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

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

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

  6. 008 String to Integer (atoi) [Leetcode]

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

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

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

  8. 在C ++中将String转换为Integer并将Integer转换为String

    In this article, we will understand how to convert an integer to a string in C++. We often come acro ...

  9. java中String,int,Integer,char、double类型转换

    java中String,int,Integer,char.double类型转换----https://www.cnblogs.com/kangyu222/p/5866025.html 转载于:http ...

最新文章

  1. 5、找出100到1000内的不含9的素数,存到result.txt文件中
  2. GNN与Transformer融合促进药物发现 | 2022几何图机器学习展望
  3. Java程序内存分析:使用mat工具分析内存占用
  4. java 如何判定消息已在队列_Java面试—消息队列
  5. 可实现自动设置尺寸的图片上传类
  6. java简单系统_Java简单学生管理系统
  7. 进程调度优先级JAVA_进程调度算法之短进程优先(Java)
  8. hive修改默认创建表格式
  9. bash脚本之条件判断
  10. mysql 使用内置函数来进行模糊搜索(locate()等)
  11. MongoDB:管道操作
  12. 猴年猴赛雷,曙光服务器全面升级E5-v4平台
  13. 李清照最经典的10首诗词
  14. 萧邦主的技术博客导航
  15. U74LVC1G07G-SOT353R-TG
  16. js 实现模糊搜索功能
  17. mysql集群重启失败_Mysql集群重启失败
  18. AJ-Report小白配置大屏手册
  19. 腾讯云服务器入门使用-docker安装
  20. 量化交易 米筐 因子分组打分(成长因子)

热门文章

  1. ABAP WRITE
  2. HiveQL与SQL区别
  3. Scala学习之爬豆瓣电影
  4. 拓展欧几里得 [Noi2002]Savage
  5. 一个极其高效的虚拟机内存冗余消除机制:UKSM
  6. Extjs中给同一个GridPanel中的事件添加参数的方法
  7. 淘宝开放API,很不错
  8. 新手学html 第一节:html简介
  9. oracle Merge 函数
  10. [译】Redux入门教程(一)