试题请參见: https://oj.leetcode.com/problems/string-to-integer-atoi/

题目概述

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.
spoilers alert...
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.

解题思路

这道题目还真是艰辛~ 看起来非常基础, 但是有些Case还真是暗藏玄机.

主要问题还是推断是否越界. 
我的做法是, 记录n = n * 10 + currentDigit运算前n的值. 若运算后n / 10和之前记录下来的值相等, 则还未越界.

源码

class Solution {
public:int atoi(const char *str) {int n = 0;bool isPositive = true;size_t i = 0;// Ignore Spacesfor ( ; str[i] == ' ' && str[i] != 0; ++ i ) { }// Process Sign Bitif ( str[i] == '+' || str[i] == '-' ) {isPositive = (str[i] == '+');++ i;}// Convert to Integerfor ( ; isDigit(str[i]) && str[i] != 0; ++ i ) {char digit = str[i] - '0';int previousResult = n;n = n * 10 + digit;// If it's Overflowif ( n / 10 != previousResult ) {if ( isPositive ) {return INT_MAX;} else {return INT_MIN;}}}return ( isPositive ? n : -n );}
private:bool isDigit(char digit) {return (digit >= '0' && digit <= '9');}
};

转载于:https://www.cnblogs.com/blfshiye/p/5114200.html

LeetCodeOJ. String to Integer (atoi)相关推荐

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

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

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

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

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

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

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

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

  5. String to Integer (atoi) leetcode java

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

  6. 8. String to Integer (atoi)

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

  7. Leet Code OJ 8. String to Integer (atoi) [Difficulty: Easy]

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

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

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

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

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

最新文章

  1. 成贤学院计算机组成原理,东南大学成贤学院计算机硬件课程设计报告解读.doc...
  2. pandas.apply 有源码github
  3. 同步方法和同步代码块
  4. 二、使用rails3.0自带的数据检查功能检查输入数据
  5. 分布式资源调度—YARN框架
  6. jQuery之换肤与cookie插件
  7. KR C与ANSI C
  8. SpringMVC上传文件的三种方式
  9. python导入自己写的py_10万行代码10万年薪,利用python查看自己写了多少代码!
  10. Linux宝库快讯 | OpenInfra中国日正式确定会议合作方
  11. java中json数组如何转为对象
  12. 使用KXML解析xml数据
  13. 串口 通讯 顶尖电子秤_串口通讯协议_电子秤的串口通讯协议解析 - 全文
  14. JAVA毕业设计共享汽车管理系统计算机源码+lw文档+系统+调试部署+数据库
  15. 【论文阅读】A Memory-Efficient Deterministic Finite Automaton-Based Bit-Split String Matching Scheme
  16. 阿里云视频直播开发----java
  17. Linux ps命令简介
  18. redo synch writes在什么情况下发生
  19. 干支纪年法简便算法_高中化学分类学法指导!附高考化学必记知识点及规律
  20. Python 使用PIL.Image制作一个运动小人的动态图

热门文章

  1. 代码单元测试:gtest
  2. 银行员工会购买自己银行的理财产品吗?
  3. 《拯救人类》:很有可能改变人类历史进程的书
  4. java不同进程的相互唤醒_Java线程生命周期与状态切换
  5. java 向上抛异常_java throws 向上抛出的概念问题
  6. python刷阅读_用Python汇集并生成每日教育动态
  7. 未声明spire。它可能因保护级别而不可访问_信息系统安全:访问控制技术概述...
  8. JQuery的ready函数与JS的onload的区别详解
  9. 31、SAM文件中flag含义解释工具--转载
  10. 久违的反省,容忍现在的自己