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. 用状态机表示SFC中的并行分支

    过去一直认为,状态机表示SFC会不会是任务复杂化,这次简单实验了一下,感觉还可以.请看下面的控制. 在SFC中,A和B是一对并行分支,汇合后转移到C分支中,怎么了用状态机表示呢?这里我们在状态机里分别 ...

  2. 004---Linux系统设置

    Linux版本相关命令 查看系统版本:cat /etc/redhat-release 查看系统内核版本以及位数:uname -r [root@hostname1 ~]# cat /etc/redhat ...

  3. Spring AOP(一)——基础概念

    前文的一些内容更多是针对Spring容器内部的一些特性的描述,接下来一个专题将描述Spring AOP的一些信息,配置细节等等. 介绍 面向切面编程(AOP)是一种新的针对程序结构的思路,它补足了面向 ...

  4. 初步学习pg_control文件之十五

    接前文  初步学习pg_control文件之十四 再看如下这个: int MaxConnections; 应该说,它是一个参考值,在global.c中有如下定义 /* * Primary determ ...

  5. springmvc 处理put,delete请求

    前言:ajax用post编辑,删除提示越权操作状态为500,修改半晌最后大神指点说是:type修改为post和delete模式 最后还是一知半解,但是程序却正常使用了.当然注意我用的mvc,contr ...

  6. c++实验3类和对象

     实 验 3: part 1:验证 part 2:graph #include <iostream> #include "graph.h" using namespac ...

  7. LINUX系统配置相关

    修改系统引导文件 grub.cfg的文件位置   /boot/grub/grub.cfg set default="4"  默认windows是在第四个选项 set timeout ...

  8. Notepad++删除空行的多种实现办法

    Notepad++支持基础的正则表达式,同时由于自身丰富的插件和功能,所以删除空行或有空格的空行,有多种实现办法,条条大路通罗马,闪电博客抛砖引玉,供大家参考. 一.删除空行(不包括有空格类符号的空行 ...

  9. APK反编译后添加日志

    一.反编译 参考前一篇文章 二.添加寄存器(locals) 因为要添加日志,我们一般需要用一个变量来存储TAG,所以需要增加一个寄存器 如: # virtual methods .method pub ...

  10. CSS3 : box-shadow

    box-shadow 用于设置边框阴影,语法: box-shadow: h-shadow v-shadow blur spread color inset; -moz-box-shadow: h-sh ...