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.

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.

题解:

  1. 跳过开始的所有空格

  2.如果有 '+'、‘-’ ,则存储正负号。

  3.如果不是数字,则直接返回 0

  4.在 3 过程中,溢出条件(前提是 res 还未添加当前遍历的数字 digit):

    1) res > INT_MAX

    2)sign == 1 && res == INT_MAX && digit > 7

    3)sign == -1 && res == INT_MIN && digit > 8

 class Solution {
public:
int myAtoi(string str) {
int res = ;
int n = str.size();
int i = ;
int sign = ;
while (i < n && str[i] == ' ') ++i;
if (i == n)
return ;
if (str[i] == '+' || str[i] == '-')
sign = (str[i++] == '-') ? - : ;
while (i < n && str[i] >= '' && str[i] <= '') {
int digit = str[i++] - '';
if (res > INT_MAX / || res == INT_MAX / && digit > )
return sign == ? INT_MAX : INT_MIN;
res = res * + digit;
} return res * sign;
}
};

【LeetCode】008. String to Integer (atoi)的更多相关文章

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

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

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

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

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

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

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

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

  5. 《LeetBook》leetcode题解(8): String to Integer (atoi) [E]——正负号处理

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

  6. 【LeetCode】字符串 string(共112题)

    [3]Longest Substring Without Repeating Characters (2019年1月22日,复习) [5]Longest Palindromic Substring ( ...

  7. No.008 String to Integer (atoi)

    8. String to Integer (atoi) Total Accepted: 112863 Total Submissions: 825433 Difficulty: Easy Implem ...

  8. LeetCode--No.008 String to Integer (atoi)

    8. String to Integer (atoi) Total Accepted: 112863 Total Submissions: 825433 Difficulty: Easy Implem ...

  9. 【LeetCode】7 & 8 - Reverse Integer & String to Integer (atoi)

    7 - Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 Notic ...

随机推荐

  1. PHP汉子转拼音

    <?php /** +------------------------------------------------------ * PHP 汉字转拼音 +------------------ ...

  2. TIJ读书笔记03-初始化和构造器

      TIJ读书笔记03-初始化和构造器 初始化和清理是涉及安全的两个问题,如果对象不能正确的初始化会引起很多错误,比如空指针异常等,如果不能恰当及时的清理,会占用过多资源. 构造器在创建一个类的实例的 ...

  3. 玩转python主题模型程序库gensim

    gensim是python下一个极易上手的主题模型程序库(topic model),网址在:http://radimrehurek.com/gensim/index.html 安装过程较为繁琐,参考h ...

  4. lvds(800*600)

    static struct fb_videomode ldb_modedb[] = { 107 107 { 108 + "LDB-SGA", 60, 800, 600, 25132 ...

  5. readonly与disabled

    readonly与disabled都能将元素设为不可编辑状态,但他们有许多区别: 1.样式 readonly与一般样式一样,disabled会将元素背景设为灰色 2.应用范围 readonly只对in ...

  6. MySQL数据库基本操作(一)

    进入mysql 本地连接: mysql -u用户名 -p 输入密码 qwe123 mysql -uroot -pqwe123 sudo apt-get install mysql-server # p ...

  7. linux crontab使用

    1.查看.编辑和删除 cron把命令行保存在crontab(cron table)文件里,这个文件通常在 /etc 目录下. 每个系统用户都可以有自己的crontab(在 /var/spool/cro ...

  8. 深入Spring:自定义注解加载和使用

    前言 在工作中经常使用Spring的相关框架,免不了去看一下Spring的实现方法,了解一下Spring内部的处理逻辑.特别是开发Web应用时,我们会频繁的定义@Controller,@Service ...

  9. js实现级联菜单(没有后台)

    html代码: <!-- js级联菜单 --> <div id="cascMenu"> <select id="select" o ...

  10. spark学习3(sqoop1.4.6安装)

    sqoop目前有两个版本sqoop1和sqoop2,这里安装的是sqoop1版本 1)将软件上传到spark1节点 2)修改权限 [root@spark1 sqoop]# chmod u+x sqoo ...