【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 ...
随机推荐
- RAID 磁盘状态为foreign,怎么变成ready
我们在关掉电源,插入三块硬盘,打开电源 这时候: 首先启动服务器,出现[Press<ctrl><R>to Run Configuration Utility]提示时,按下ctr ...
- iOS边练边学--NSURLSession、NSURLSessionTask的介绍与使用以及url中包含了中文的处理方法
一.NSURLSession.NSURLSessionTask的使用步骤 首先创建NSURLSession对象 通过NSURLSession对象创建对应的任务 <1>NSURLSessio ...
- 洛谷P1203 [USACO1.1]坏掉的项链Broken Necklace
题目描述 你有一条由N个红色的,白色的,或蓝色的珠子组成的项链(3<=N<=350),珠子是随意安排的. 这里是 n=29 的二个例子: 第一和第二个珠子在图片中已经被作记号. 图片 A ...
- 洛谷P3386 【模板】二分图匹配
匈牙利算法模板 /*by SilverN*/ #include<algorithm> #include<iostream> #include<cstring> #i ...
- a[1000][1000]程序崩溃
1000 * 1000是大于65536的.如果不是需求需要,没必要开辟如此之多的空间.因为这些空间实在栈上申请的(如果是局部变量),栈的空间是有限的并且是宝贵的,所以呢,开辟太多的空间而不适用很可能会 ...
- Android Studio集成SVN报错:can't use subversion command line client : svn
Android Studio集成SVN插件,check out出代码后,每次开启都会在右上角出现如下错误: Can't use Subversion command line client: svn ...
- Oracle闪回技术之一Oracle 11g 利用FlashTable (闪回表)恢复(用delete)误删的数据
闪回表,实际上就是将表中的数据快速恢复到过去的一个时间点或者系统改变号SCN上.实现表的闪回,需要用到撤销表空间相关的UNDO信息,通过SHOW PARAMETER UNDO命令就可以了解这些信息.用 ...
- 使用servletAPI三种方式简单示例
一.直接实现Action接口或集成ActionSupport类(推荐) public class HelloAction implements Action { @Override public St ...
- The mean shift clustering algorithm
The mean shift clustering algorithm MEAN SHIFT CLUSTERING Mean shift clustering is a general non-par ...
- hdu 1261 字串数
解题思路:ACM紫书 第十章 P319 有重复元素的全排列 答案: 所有数的和的阶乘 除以 每个数阶乘的乘积 因为给定 (26*12)! 会爆掉(long long),这里用java 的BigInte ...