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.

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 字符串转换成整数的更多相关文章

  1. 【LeetCode每天一题】String to Integer (atoi)(字符串转换成数字)

    Implement atoi which converts a string to an integer.The function first discards as many whitespace ...

  2. 8. String to Integer (atoi) 字符串转成整数

    [抄题]: Input: "42" Output: 42 Example 2: Input: " -42" Output: -42 Explanation: T ...

  3. 【LeetCode】8. String to Integer (atoi) 字符串转换整数

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:字符串转整数,atoi,题解,Leetcode, 力扣,P ...

  4. 【LeetCode】String to Integer (atoi)(字符串转换整数 (atoi))

    这道题是LeetCode里的第8道题. 题目要求: 请你来实现一个 atoi 函数,使其能将字符串转换成整数. 首先,该函数会根据需要丢弃无用的开头空格字符,直到寻找到第一个非空格的字符为止. 当我们 ...

  5. [LeetCode]面试题67. 把字符串转换成整数

    题目 写一个函数 StrToInt,实现把字符串转换成整数这个功能.不能使用 atoi 或者其他类似的库函数. 首先,该函数会根据需要丢弃无用的开头空格字符,直到寻找到第一个非空格的字符为止. 当我们 ...

  6. [LeetCode] String to Integer (atoi) 字符串转为整数

    Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. ...

  7. [LeetCode] String to Integer (atoi) 字符串

    Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. ...

  8. StringToInt(atoi) 字符串转换成整数

    public class StringToInt { public int atoi(String s) { long num = 0; int minus = 0; if(s==null) { re ...

  9. 算法练习-字符串转换成整数(实现atoi函数)

    练习问题来源 https://leetcode.com/problems/string-to-integer-atoi/ https://wizardforcel.gitbooks.io/the-ar ...

随机推荐

  1. js中的逻辑与和逻辑或随笔

    逻辑与:&&,都真才真 逻辑或:||,一真都真 逻辑运算两侧不都是布尔值时,会隐式转换为布尔值转换规则:转换为true:非0数字(包含infinity).非空字符串转换为false:0 ...

  2. LINUX SSH 建立密钥对

    配置私钥和公钥 先检查一下服务器的ssh配置文件 /etc/ssh/sshd_config RSAAuthentication yes # 启用 RSA 认证 默认为 yes PubkeyAuthen ...

  3. ZooKeeper(3)-内部原理

    一. 节点类型 二. Stat结构体 1)czxid-创建节点的事务zxid 每次修改ZooKeeper状态都会收到一个zxid形式的时间戳,也就是ZooKeeper事务ID. 事务ID是ZooKee ...

  4. DJANGO2.0 关联表的必填 ON_DELETE

    DJANGO2.0 关联表的必填 ON_DELETE 参数的含义 - BUXIANGHEJIU 的博客 - CSDN 博客 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blo ...

  5. Leecode刷题之旅-C语言/python-83删除排序链表中的重复元素

    /* * @lc app=leetcode.cn id=83 lang=c * * [83] 删除排序链表中的重复元素 * * https://leetcode-cn.com/problems/rem ...

  6. 责任链模式的使用-Netty ChannelPipeline和Mina IoFilterChain分析

    本文来自网易云社区 作者:乔安然 1. Chain of Responsiblity 定义: 使多个对象都有机会处理请求,从而避免请求的发送者和接受者之间的耦合关系.将这个对象连成一条链,并沿着这条链 ...

  7. GreenMail邮件测试服务器

    GreenMail邮件测试服务器 http://blog.csdn.net/jackiehff/article/details/8741988 这个目前没有需求,所以暂不研究

  8. mysql分布式技术

    所有的分布式技术 dobble zokkiper ngix

  9. Drools 7.4.1.Final参考手册(八) 规则语言参考

    规则语言参考 概述 Drools有一个“本地”的规则语言.这种格式在标点符号上非常轻,并且通过“扩展器”支持自然语言和领域特定的语言,使语言能够变形到您的问题领域.本章主要与本机规则格式一致.用于表示 ...

  10. HDU 4467 Graph(图论+暴力)(2012 Asia Chengdu Regional Contest)

    Description P. T. Tigris is a student currently studying graph theory. One day, when he was studying ...