[Leetcode] 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.
题意:将字符串转换成整数,需要注意的几点如下:
1)从字符串开头开始遍历,若开始为空格,则跳过,直到第一个非空格的字符,没有非空格字符,则返回0,;
2)若第一个非空的字符为正负号,则最后返回的数考虑正负号;
3)若接下来的字符不是数字,则返回0. 完全不考虑小数点和自然数的情况;
4)继续遍历中,遇到数字则转换成整数保存下来,若再次遇到非数字型的字符,则返回当前保存的值;
5)遍历过程要考虑值的返回,若是超过整型类型的范围,则返回边界值;
参考了,zhouworld16 ,代码如下:
class Solution {
public:
int atoi(const char *str)
{
if(str==NULL) return ;
long long res=;
int i=;
bool flag=true;
while(str[i]==' '||str[i]=='')
i++;
if(str[i]=='+')
i++;
if(str[i]=='-')
{
flag=false;
i++;
}
int len=strlen(str);
for(;i<len;++i)
{
if(str[i]>=''&&str[i]<='')
{
res=res*+(str[i]-'');
if(res>INT_MAX)
return flag?INT_MAX:INT_MIN;
}
else
{
return flag?res:(-)*res;
}
}
return flag?res:(-)*res;
}
};
值得注意的是,res的类型应该是范围比int大的类型,因为当字符串为"2147483648"仅比INT_MAX大1时,由于res只能保存2147483647,此时会产生溢出。返回-2147483648。所以res的类型要扩展,不这样做的话,参考Grandyang的博客
[Leetcode] String to integer atoi 字符串转换成整数的更多相关文章
- 【LeetCode每天一题】String to Integer (atoi)(字符串转换成数字)
Implement atoi which converts a string to an integer.The function first discards as many whitespace ...
- 8. String to Integer (atoi) 字符串转成整数
[抄题]: Input: "42" Output: 42 Example 2: Input: " -42" Output: -42 Explanation: T ...
- 【LeetCode】8. String to Integer (atoi) 字符串转换整数
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:字符串转整数,atoi,题解,Leetcode, 力扣,P ...
- 【LeetCode】String to Integer (atoi)(字符串转换整数 (atoi))
这道题是LeetCode里的第8道题. 题目要求: 请你来实现一个 atoi 函数,使其能将字符串转换成整数. 首先,该函数会根据需要丢弃无用的开头空格字符,直到寻找到第一个非空格的字符为止. 当我们 ...
- [LeetCode]面试题67. 把字符串转换成整数
题目 写一个函数 StrToInt,实现把字符串转换成整数这个功能.不能使用 atoi 或者其他类似的库函数. 首先,该函数会根据需要丢弃无用的开头空格字符,直到寻找到第一个非空格的字符为止. 当我们 ...
- [LeetCode] String to Integer (atoi) 字符串转为整数
Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. ...
- [LeetCode] String to Integer (atoi) 字符串
Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. ...
- StringToInt(atoi) 字符串转换成整数
public class StringToInt { public int atoi(String s) { long num = 0; int minus = 0; if(s==null) { re ...
- 算法练习-字符串转换成整数(实现atoi函数)
练习问题来源 https://leetcode.com/problems/string-to-integer-atoi/ https://wizardforcel.gitbooks.io/the-ar ...
随机推荐
- 转:python教程专题资源免费下载整理合集收藏
python教程专题资源免费下载整理合集收藏 < Python学习手册(第4版)>(Learning Python, 4th Edition)[PDF] 94MB 简体中文 <Pyt ...
- 糖果 南阳acm589
糖果 时间限制:1000 ms | 内存限制:65535 KB 难度:2 描述 topcoder工作室的PIAOYIi超级爱吃糖果,现在他拥有一大堆不同种类的糖果,他准备一口气把它们吃完,可是 ...
- itchat和matplotlib的结合使用爬取微信信息
前几天无意中看到了一片文章,<一件有趣的事:我用 Python 爬了爬自己的微信朋友>,这篇文章写的是使用python中的itchat爬取微信中朋友的信息,其中信息包括,昵称.性别.地理位 ...
- SQLServer 将日期改造成标准日期格式(如: 2016/6 ->201606)
同事给了份Excel 数据,导到数据库之后再查出来时发现顺序不好弄.于是想从数据源中做处理. 由于数据存在,年/月 与 年/月/日 的格式不好用datetime保存,于是用varchar保存. 数据处 ...
- python--基本类型之列表
Lest(列表): 定义和创建列表: 列表:是python以及其他语言中最常用的数据结构之一.python用 [] 来解析列表列表是可变的.--可以改变列表的内容可以用切片 a=['张三','李四', ...
- 局域网访问不到linux下的tomcat
问题描述: CentOS安装完成Tomcat后,访问本地:http://localhost:8080/正确.但局域网内无法访问,而且服务器可ping通 经查原因为防火墙开启: [root@localh ...
- 【转】iOS库 .a与.framework区别
转自:http://blog.csdn.net/lvxiangan/article/details/43115131 一.什么是库? 库是共享程序代码的方式,一般分为静态库和动态库. 二.静态库与动态 ...
- autofac无法解析一例
在asp.net mvc分项目开发中,如果类库位于其他的项目中,则必须在global中对其他项目的类库进行注册,否则会报“ None of the constructors found with 'A ...
- 最后一片蓝海的终极狂欢-写在Win10发布前夕
作为一名Windows8.x+系统平台从业者,从工作伊始,耳边不断充斥着Windows将走向没落的言论,Win10今日晚些时候即将发布,笔者借此机会,说说自己的看法. 早在2012年的时候,IDC曾预 ...
- 1.爬虫 urlib库讲解 Handler高级用法
在前面我们总结了urllib库的 urlopen()和Request()方法的使用,在这一小节我们要使用相关的Handler来实现代理.cookies等功能. 写在前面: urlopen()方法不支持 ...