字符串转成整型(int)
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)的更多相关文章
- 字符串转换成整型,到底使用int.Parse,Convert.ToInt32还是int.TryParse?
当我们想把一个字符串转换成整型int的时候,我们可能会想到如下三种方式:int.Parse,Convert.ToInt32和int.TryParse.到底使用哪种方式呢? 先来考虑string的可能性 ...
- python将字符串转换成整型
将字符串转换成,整型,从字面理解很容易让人误会. 比如,要把这个"abcabc"转换成整型,臣妾做不到啊.除成转成ascii. 我们所说字符串转成整型是这样的. s = " ...
- 【转载】 C#中使用int.TryParse方法将字符串转换为整型Int类型
在C#编程过程中,将字符串string转换为整型int过程中,时常使用的转换方法为int.Parse方法,但int.Parse在无法转换的时候,会抛出程序异常,其实还有个int.TryParse方法可 ...
- 【转载】C#中使用int.Parse方法将字符串转换为整型Int类型
在C#编程过程中,很多时候涉及到数据类型的转换,例如将字符串类型的变量转换为Int类型就是一个常见的类型转换操作,int.Parse方法是C#中专门用来将字符串转换为整型int的,int.Parse方 ...
- C#中IPAddress转换成整型int
string addr = "11.22.33.44"; System.Net.IPAddress IPAddr=System.Net.IPAddress.Parse(addr); ...
- VC++中如何将字符串转换成整型数字
原文:http://blog.csdn.net/yongf2014/article/details/47071663 注意: atoi函数是c的函数,它的输入参数是char *类型. 你声明了stri ...
- wid是一个字符串 必须转化成整型
wid是一个字符串 必须转化成整型
- Java:将字符串中的数字转换成整型
在C语言中,将字符串中的数字转换为整型的方法是是利用atoi这个函数.在Java中,我们可以利用parseInt方法来实现,具体代码如下: public class HelloWorld { publ ...
- 基础数据类型:整型int、布尔值bool、字符串str、与for循环
1.整型 int() p2 long 长整型 p3 全部都是整型 2.布尔值 bool() True --- int() int(True) int() --- True bool(int) 注意点: ...
随机推荐
- IP地址输入框
<style> div.IPDiv{background:#ffffff;width:120;font-size:9pt;text-align:center;border:2 ridge ...
- es6导入导出模块
在JavaScript ES6中,export与export default均可用于导出常量.函数.文件.模块等,你可以在其它文件或模块中通过import+(常量 | 函数 | 文件 | 模块)名的方 ...
- DDL DML DCL DQL的区别
原文章出处:http://blog.csdn.net/tomatofly/article/details/5949070 SQL(Structure Query Language)语言是数据库的核心语 ...
- Mock(模拟后端接口数据)配合Vuex的使用
1.下载Mock cnpm install Mockjs -S 2.新建一个data.js存放新生成的mock文件 编辑mock 并导出 const Mock = require('mockjs' ...
- 洛谷——P2071 座位安排 seat.cpp/c/pas
P2071 座位安排 seat.cpp/c/pas 题目背景 公元二零一四年四月十七日,小明参加了省赛,在一路上,他遇到了许多问题,请你帮他解决. 题目描述 已知车上有N排座位,有N*2个人参加省赛, ...
- brpc初探
因为最近在看一个内部开源代码,看到了braft.braft又依赖于brpc.于是就看了相关的文档,打算接下来试一把. 这里引用下gejun大佬在知乎上的回答(https://www.zhihu.com ...
- HDU 1272(并查集)
小希的迷宫 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submi ...
- 【BZOJ 3133】 3133: [Baltic2013]ballmachine (线段树+倍增)
3133: [Baltic2013]ballmachine Time Limit: 20 Sec Memory Limit: 128 MBSubmit: 148 Solved: 66 Descri ...
- Circular dependencies cannot exist in RelativeLayout
循环布局错误!!! <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:an ...
- WPF TreeGrid的一种简单实现
前几天,各种坑爹的情况,导致不得不自己去动手实现带层级关系的Grid.之后翻了翻书,貌似说msdn和codeproject上有这么个例子,叫做TreeListView.这里就简单说下自己的思路,也许有 ...