1 题目

Implement atoito convert a string to an integer.

Hint: Carefullyconsider all possible input cases. If you want a challenge, please do not seebelow and ask yourself what are the possible input cases.

Notes: It isintended for this problem to be specified vaguely (ie, no given input specs).You are responsible to gather all the input  requirements up front.

Requirements for atoi:

The functionfirst discards as many whitespace characters as necessary until the firstnon-whitespace character is found. Then, starting from this character, takes anoptional initial plus or minus sign followed by as many numerical digits aspossible, and interprets
them as a numerical value.

The string cancontain additional characters after those that form the integral number, whichare ignored and have no effect on the behavior of this function.

If the firstsequence of non-whitespace characters in str is not a valid integral number, orif no such sequence exists because either str is empty or it contains onlywhitespace characters, no conversion is performed.

If no validconversion could be performed, a zero value is returned. If the correct valueis out of the range of representable values, INT_MAX (2147483647) or INT_MIN(-2147483648) is returned.

2 分析

(1) 空字符串返回0;

(2) 去除字符串前边的0;

(3) 若遇到非数字字符则终止计算;

(4) 若求出的数越界则返回最大值或者最小值。

3 实现

int atoi(string str)
{
int start = str.find_first_not_of(' ');
if (start > 0)
{
str = str.substr(start);
}
int size = str.size();
if (0 == size)
{
return 0;
} int signal = 1;
int i = 0;
if (str[i] == '+')
{
++i;
}
else if (str[i] == '-')
{
signal = -1;
++i;
} long long res = 0;
while (i < size)
{
if (str[i] < '0' || str[i] > '9')
{
break;
}
res = res * 10 + (str[i] - '0');
if (res > INT_MAX)
{
return signal == 1 ? INT_MAX : INT_MIN;
}
++i;
} return res * signal;
}

字符串转成整型(int)的更多相关文章

  1. 字符串转换成整型,到底使用int.Parse,Convert.ToInt32还是int.TryParse?

    当我们想把一个字符串转换成整型int的时候,我们可能会想到如下三种方式:int.Parse,Convert.ToInt32和int.TryParse.到底使用哪种方式呢? 先来考虑string的可能性 ...

  2. python将字符串转换成整型

    将字符串转换成,整型,从字面理解很容易让人误会. 比如,要把这个"abcabc"转换成整型,臣妾做不到啊.除成转成ascii. 我们所说字符串转成整型是这样的. s = " ...

  3. 【转载】 C#中使用int.TryParse方法将字符串转换为整型Int类型

    在C#编程过程中,将字符串string转换为整型int过程中,时常使用的转换方法为int.Parse方法,但int.Parse在无法转换的时候,会抛出程序异常,其实还有个int.TryParse方法可 ...

  4. 【转载】C#中使用int.Parse方法将字符串转换为整型Int类型

    在C#编程过程中,很多时候涉及到数据类型的转换,例如将字符串类型的变量转换为Int类型就是一个常见的类型转换操作,int.Parse方法是C#中专门用来将字符串转换为整型int的,int.Parse方 ...

  5. C#中IPAddress转换成整型int

    string addr = "11.22.33.44"; System.Net.IPAddress IPAddr=System.Net.IPAddress.Parse(addr); ...

  6. VC++中如何将字符串转换成整型数字

    原文:http://blog.csdn.net/yongf2014/article/details/47071663 注意: atoi函数是c的函数,它的输入参数是char *类型. 你声明了stri ...

  7. wid是一个字符串 必须转化成整型

    wid是一个字符串 必须转化成整型

  8. Java:将字符串中的数字转换成整型

    在C语言中,将字符串中的数字转换为整型的方法是是利用atoi这个函数.在Java中,我们可以利用parseInt方法来实现,具体代码如下: public class HelloWorld { publ ...

  9. 基础数据类型:整型int、布尔值bool、字符串str、与for循环

    1.整型 int() p2 long 长整型 p3 全部都是整型 2.布尔值 bool() True --- int() int(True) int() --- True bool(int) 注意点: ...

随机推荐

  1. thinkphp5.0架构总览

    ThinkPHP5.0应用基于MVC(模型-视图-控制器)的方式来组织. MVC是一个设计模式,它强制性的使应用程序的输入.处理和输出分开.使用MVC应用程序被分成三个核心部件:模型(M).视图(V) ...

  2. CodeForces 811C Vladik and Memorable Trip

    $dp$. 记录$dp[i]$表示以位置$i$为结尾的最大值. 枚举最后一段是哪一段,假设为$[j,i]$,那么可以用$max(dp[1]...dp[j-1]) + val[j][i]$去更新$dp[ ...

  3. SQL2:数据操作

    1.数据插入: 1)插入表中: INSERT INTO TABLE_NAME VALUE('value1','value2',....); 2)从另一个表中插入数据: INSERT INTO TABL ...

  4. 在学习HTML——form表单中的label标签时的一点小体会

    在我啃了一遍书本之后,开始了在慕课看视频的过程,从最开始的HTML+CSS的基础课程看起,在第5-9小节讲到了form表单的label标签, 首先看一下慕课的讲解:  label 标签不会向用户呈现任 ...

  5. 【BZOJ 4170】 4170: 极光 (CDQ分治)

    4170: 极光 Time Limit: 30 Sec  Memory Limit: 512 MBSubmit: 121  Solved: 64 Description "若是万一琪露诺(俗 ...

  6. BZOJ1017 魔兽地图DotR (树上背包)

    一道背包的神题,用到了树上dp和背包dp,这个题的特殊性在于儿子对于父亲节点是有影响的,所以用f[i][j][k]表示第i号装备,其中用j个来合成上层装备,花费k元所能获得最大的力量值. 然后对于每一 ...

  7. [ARC055D]隠された等差数列

    题意:对一个等差数列$a_i=A+Bi(0\leq i\leq n-1)$和非负整数$x$,把$a_i$的$10^x$位拿出来可以写成一个字符集为$0\cdots9$的字符串,现在给定这个字符串$d_ ...

  8. Android消息机制——Handler

      /**android的消息处理有三个核心类:Looper,Handler和Message.其实还有一个MessageQueue(消息队列), * 但是MessageQueue被封装到Looper里 ...

  9. DP练习 最长上升子序列nlogn解法

    openjudge 百练 2757:最长上升子序列 总时间限制:  2000ms 内存限制:  65536kB 描述 一个数的序列bi,当b1 < b2 < ... < bS的时候, ...

  10. centos安装jdk文件

    1.到oracle官网选择要安装的jdk版本 http://www.oracle.com/technetwork/java/javase/downloads/index-jsp-138363.html ...