【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 ...
- Spring-事物的隔离级别
Spring中定义了5中不同的事务隔离级别: 1. ISOLATION_DEFAULT(一般情况下使用这种配置既可) ; 这是一个PlatfromTransactionManager默认的隔离级别,使 ...
- codeforces 719A:Vitya in the Countryside
Description Every summer Vitya comes to visit his grandmother in the countryside. This summer, he go ...
- SQL Server数据库还原:"因为数据库正在使用,所以无法获得对数据库的独占访问权"
如题,网上找了一些客套的方法,如果不想去折腾,请看我的方法: 1.先脱机数据库,这个目的就是为了停掉所有链接 2.选择还原数据库,如果提示日志尾部不完整,请选择数据库属性的选项,覆盖现有数据. 还可以 ...
- eclipse中运行python脚本中有注释为中文的内容,报错:SyntaxError: Non-ASCII character '\xe5'
'''Created on 2015年7月2日 @author: liujuan'''import sysreload(sys) 以上为注释的有个日期中文的,结果运行报错:SyntaxError: N ...
- 5种风格的 jQuery 分页效果【附代码】
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xht ...
- 找出进程中各线程cpu消耗情况
以root用户执行以下命令,以PID 5423举例: 1,根据top命令,找到占用CPU高的进程,找到PID PID USER PR NI VIRT RES SHR S %CPU ...
- OOA/OOD/OOP(了解)
Object-Oriented Analysis:面向对象分析方法 是在一个系统的开发过程中进行了系统业务调查以后,按照面向对象的思想来分析问题.OOA与结构化分析有较大的区别.OOA所强调的是在系统 ...
- ECSHOP手机号码或邮箱用户名都可以登录方法
ECSHOP手机号码或邮箱用户名都可以登录方法 ECSHOP教程/ ecshop教程网(www.ecshop119.com) 2013-06-30 有不少人都在找支持ECShop用户名.邮箱或手号 ...
- Hbase Shell常用命令
hbase shell常用的操作命令有create,describe,disable,drop,list,scan,put,get,delete,deleteall,count,status等,通过h ...