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)的更多相关文章
-
【LeetCode】8. String to Integer (atoi) 字符串转换整数
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:字符串转整数,atoi,题解,Leetcode, 力扣,P ...
-
【LeetCode】8. String to Integer (atoi) 字符串转整数
题目: Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input ca ...
-
【leetcode】8. String to Integer (atoi)
题目描述: Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input ...
-
【一天一道LeetCode】#8. String to Integer (atoi)
一天一道LeetCode系列 (一)题目 Implement atoi to convert a string to an integer. Hint: Carefully consider all ...
-
《LeetBook》leetcode题解(8): String to Integer (atoi) [E]——正负号处理
我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...
-
【LeetCode】字符串 string(共112题)
[3]Longest Substring Without Repeating Characters (2019年1月22日,复习) [5]Longest Palindromic Substring ( ...
-
No.008 String to Integer (atoi)
8. String to Integer (atoi) Total Accepted: 112863 Total Submissions: 825433 Difficulty: Easy Implem ...
-
LeetCode--No.008 String to Integer (atoi)
8. String to Integer (atoi) Total Accepted: 112863 Total Submissions: 825433 Difficulty: Easy Implem ...
-
【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 ...
随机推荐
-
JAVAWEB学习总结 HTTPSERVLETRESPONSE对象(二)
一.HttpServletResponse常见应用--生成验证码 1.1.生成随机图片用作验证码 生成图片主要用到了一个BufferedImage类 步骤: 1. 在内存中创建一张图片 2.得到图片 ...
-
一、IOS运行原理
1.首先执行main函数 2.执行UIPaalicationMain函数 3.UIApplication函数内部 1>创建一个UIApplication实例.这个UIApplication对象是 ...
-
Microsoft Azure 的一些限制 Global
Azure Subscription and Service Limits, Quotas, and Constraints http://azure.microsoft.com/en-us/docu ...
-
.net core学习
http://www.cnblogs.com/artech/ http://www.blogs8.cn/posts/AA0E630 http://pan.baidu.com/s/1bo4fJ47 ht ...
-
Axure RP 实践.1
工作需要设计产品原型,找来Axure RP帮忙,看了一些文章,其中下面这段话深得我心. “只使用Axure的默认控件(Wireframe),不要用那些样式花哨的自定义控件,并且所有页面中使用的颜色不能 ...
-
[转帖]为应用程序池“XXX”提供服务的进程在与 Windows Process Activation Service 通信时出现严重错误。该进程 ID 为“XXXX”。数据字段包含错误号。
[终极解决方案]为应用程序池“XXX”提供服务的进程在与 Windows Process Activation Service 通信时出现严重错误.该进程 ID 为“XXXX”.数据字段包含错误号. ...
-
Hdu2041 超级楼梯 (斐波那契数列)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2041 超级楼梯 Time Limit: 2000/1000 MS (Java/Others) M ...
-
Ng第七课:正则化与过拟合问题 Regularization/The Problem of Overfitting
7.1 过拟合的问题 7.2 代价函数 7.3 正则化线性回归 7.4 正则化的逻辑回归模型 7.1 过拟合的问题 如果我们有非常多的特征,我们通过学习得到的假设预测可能能够非常好地适应训练 ...
-
linux shell 脚本攻略学习4
1.cat命令详解 cat 是concatnate(拼接)的简写. 语法: cat file1 file2 file3 .... 作用:将文件内容拼接在一起进行输出 具体应用: 1).压缩空白行 加上 ...
-
树形DP总结,持续更新
自己做了动态规划的题目已经有了一个月,但是成效甚微,所以来总结一下动态规划,希望自己能够温故知新.这个博客是关于树形dp的,动态规划的一类题目. 首先从最简单的树形DP入手,树形DP顾名思义就是一棵树 ...