【leetcode】String to Integer (atoi)
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... click to show 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.
atoi的原理:
1.如果字符串前面有空格,则去除所有空格
2.允许有正负号,需要读取出正负号,连续两个正负号则是错误的字符串
3.读取过正负号后,开始读取数字,直到读到不是数字的位置或者字符串结束时停止,解析出里面的数字
注意会溢出,溢出时返回INT_MAX或者INT_MIN
class Solution {
public:
int atoi(const char *str) {
if(str==NULL) return ;
int result=;
int flag=;
while(*str==' ')
{
str++;
}
if(*str=='+')
{
flag=;
str++;
}
else if(*str=='-')
{
flag=-;
str++;
}
while(*str>=''&&*str<='')
{ int digit=*str-'';
if(result<=INT_MAX/)
{
result*=;
}
else
{
if(flag==) return INT_MAX;
else return INT_MIN;
} if(result<=INT_MAX-digit)
{
result+=digit;
}
else
{
if(flag==) return INT_MAX;
else return INT_MIN;
} str++;
} return flag*result;
}
};
【leetcode】String to Integer (atoi)的更多相关文章
- 【LeetCode】String to Integer (atoi) 解题报告
这道题在LeetCode OJ上难道属于Easy.可是通过率却比較低,究其原因是须要考虑的情况比較低,非常少有人一遍过吧. [题目] Implement atoi to convert a strin ...
- 【LeetCode】String to Integer (atoi)(字符串转换整数 (atoi))
这道题是LeetCode里的第8道题. 题目要求: 请你来实现一个 atoi 函数,使其能将字符串转换成整数. 首先,该函数会根据需要丢弃无用的开头空格字符,直到寻找到第一个非空格的字符为止. 当我们 ...
- LeetCode【8】. String to Integer (atoi) --java实现
String to Integer (atoi) Implement atoi to convert a string to an integer. Hint: Carefully consider ...
- 【Leetcode】【Easy】String to Integer (atoi)
Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. ...
- 【LeedCode】String to integer(atoi)
Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. ...
- 【Leetcode-easy】String to Integer(atoi)
题目要求:字符串->整型 * 1. 首先需要丢弃字符串前面的空格. * 2. 然后可能有正负号(注意只取一个,如果有多个正负号,那么说这个字符串是无法转换的,返回0.比如测试用例里就有个“+-2 ...
- Leetcode 8. String to Integer (atoi) atoi函数实现 (字符串)
Leetcode 8. String to Integer (atoi) atoi函数实现 (字符串) 题目描述 实现atoi函数,将一个字符串转化为数字 测试样例 Input: "42&q ...
- 【Leet Code】String to Integer (atoi) ——常考类型题
String to Integer (atoi) Total Accepted: 15482 Total Submissions: 106043My Submissions Implement ato ...
- leetcode day6 -- String to Integer (atoi) && Best Time to Buy and Sell Stock I II III
1. String to Integer (atoi) Implement atoi to convert a string to an integer. Hint: Carefully con ...
随机推荐
- Html设置图标icon
html head添加: <link rel="icon" href="/favicon.ico" type="image/x-icon&quo ...
- 整数划分 (区间DP)
整数划分(四) 时间限制:1000 ms | 内存限制:65535 KB 难度:3 描述 暑假来了,hrdv 又要留学校在参加ACM集训了,集训的生活非常Happy(ps:你懂得),可是他最近 ...
- Java-优化技术
常用的: 1.优化循环.通过重新组织重复的子表达式来提高循环体的运行性能. 2减少使用对象的数量来提高运行性能. 3.缩减网络传输数据来缩短等待时间. 其他: 1.采用对象池技术,提高对象的利用效率. ...
- Lucene 4.7 --创建索引
Lucene的最新版本和以前的语法或者类名,类规定都相差甚远 0.准备工作: 1). Lucene官方API http://lucene.apache.org/core/4_7_0/index.htm ...
- BZOJ-3668 起床困难综合症 位运算+贪心
faebdc学长杂题选讲中的题目...还是蛮简单的...位运算写的不熟练... 3668: [Noi2014]起床困难综合症 Time Limit: 10 Sec Memory Limit: 512 ...
- poj 2155 Matrix---树状数组套树状数组
二维树状数组模版,唯一困难,看题!!(其实是我英语渣) Matrix Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 22098 ...
- IOS基础之 (一) OC基础语法
一 OC语法 1.关键字 基本上所有关键字都是以@开头,比如: @interface , @implementation, @end, @public, @protected, @private 2. ...
- soa vs cop
soa强调分层:底层为高层提供服务: cop强调分块:有明确的职责和服务提供接口,为外部提供服务. SOA 原则非常强调将服务使用者和服务提供者分离开来,关于此类分离实际的含义,有很多不正式但非常有用 ...
- 移动端bug总结
1. 移动端按钮点击蓝框去除 button,input[type="button"] { outline:none; }
- jsp页面显示数据库乱码
如何页面是utf-8,数据库也是的话,页面显示数据库乱码的话,就是数据库的格式有问题