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. Loadrunder之脚本篇——参数类型

    Internal data Date/Time,Group Name,Iteration Number,Load Generator Name,Ramdom Number,Table,Unique N ...

  2. c# 接口(interface)与接口应用

    using System; using System.Collections.Generic; using System.Linq; using System.Text; //接口(interface ...

  3. Linux权限管理 ACL权限

    ACL权限简介 在普通权限中,用户对文件只有三种身份ugo,分别为属主(u).属组(g)和其他人(o):每种用户身份拥有读(read).写(write)和执行(execute)三种权限.但是在实际工作 ...

  4. 教你在树莓派使用上RTC实时时钟,不用再担心断电后时间归零的问题,开机后自动同步RTC时钟!!!

    准备工作:1.系统建议使用官方最新的镜像文件 2.RTC时钟模块板(I2C接口)建议使用DS1307时钟模块,或者RTC时钟模块RTC时钟模块: 大家知道arduino的电平是5V,树莓派是3.3V, ...

  5. linux下安装mysql遇到的一些问题

    安装命令: groupadd mysql useradd -r -g mysql -s /bin/false mysql cd /usr/local tar zxvf /path/to/mysql-V ...

  6. Wi-Fi基带芯片和Wi-Fi无线网卡设计方案

    转:http://wenku.baidu.com/link?url=Q0ImC 0IIG7qrbB8DpGrrU3aOYvxNYCyHsxukspH8XMCDYMjYMPSJq_TCISC5amsNY ...

  7. NSCoder

    person.h头文件内容 #import <Foundation/Foundation.h> @interface Person : NSObject { NSString *name; ...

  8. 数据库系统概论学习3-SQL 语句和关系代数(一)SQL 入门

    3. SQL 语句和关系代数(一)SQL 入门 3.1 数据库的编程语言 SQL 的优点 SQL 集成了数据查询(data query).数据操作(data manipulation).数据定义(da ...

  9. tree 命令【转】

    本文转载自:http://www.jb51.net/LINUXjishu/283874.html linux下怎么用tree命令以树形结构显示文件目录结构?tree命令可以以树形结构显示文件目录结构, ...

  10. 搭建配置cacti,采集信息监控

    安装cactilamp环境[iyunv@Cacti ~]#service iptables stop //关闭防火墙服务[iyunv@Cacti ~]#chkconfig iptables off / ...