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

  1. class Solution {
  2. public:
  3. int myAtoi(string str) {
  4. int res = ;
  5. int n = str.size();
  6. int i = ;
  7. int sign = ;
  8. while (i < n && str[i] == ' ') ++i;
  9. if (i == n)
  10. return ;
  11. if (str[i] == '+' || str[i] == '-')
  12. sign = (str[i++] == '-') ? - : ;
  13. while (i < n && str[i] >= '' && str[i] <= '') {
  14. int digit = str[i++] - '';
  15. if (res > INT_MAX / || res == INT_MAX / && digit > )
  16. return sign == ? INT_MAX : INT_MIN;
  17. res = res * + digit;
  18. }
  19.  
  20. return res * sign;
  21. }
  22. };

【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. css小知识---input输入块

    对于如下的界面,介绍一种实现方式. 可以将整个界面分为三块,左中右.通过display: inline-block;和float: right;左右浮动效果实现. 代码如下: <!DOCTYPE ...

  2. python__Django 分页

    自定义分页的类: #!/usr/bin/env python # -*- coding: utf-8 -*- # Created by Mona on 2017/9/20 from django.ut ...

  3. awk的控制语句

    本章主要讲actions中的控制语句,和C语言的控制语句类似. 1.选择语句 if (condition) then-body else else-body 2.循环语句之while: while ( ...

  4. linux之下载工具wget

    常用格式:wget options URL -b,  --background        启动后转入后台执行 -c,  --continue               接着下载没下载完的文件 - ...

  5. java 命令行

    javac 编译 linux平台下:javac -cp ./hadoop-common-2.7.1.jar:./hadoop-mapreduce-client-core-2.7.4.jar: Word ...

  6. hive学习5(复制表结构)

    hive复制表结构 CREATE TABLE new_table LIKE old_table; 例:创建一个和stg_job表一样表结构的s_job表 create table s_job like ...

  7. Boostarp-响应式

    一.响应式 响应式介绍 - 响应式布局是什么? 同一个网页在不同的终端上呈现不同的布局等 - 响应式怎么实现的? 1. CSS3 media query 媒体查询 2. JS去控制网页的布局和样式等 ...

  8. Python3一些包的下载

    首先在windows的Python扩展包网址:http://www.lfd.uci.edu/~gohlke/pythonlibs/ 这里举例下载opencv3.2.0的安装包 我的电脑是win10,6 ...

  9. js状态模式

    状态模式,当一个对象的内在状态改变时允许改变其行为,这个对象看起来是改变了其类. 状态模式主要解决的是当控制一个对象状态转换的条件表达式过于复杂时的情况.把状态的判断逻辑转移到表示不同状态的一系列类当 ...

  10. WPF中的事件及冒泡事件和隧道事件(预览事件)的区别

    WPF快速指导10:WPF中的事件及冒泡事件和隧道事件(预览事件)的区别   WPF快速指导10:WPF中的事件及冒泡事件和隧道事件(预览事件)的区别 本文摘要: 1:什么是路由事件: 2:中断事件路 ...