字符串转成整型(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) 注意点: ...
随机推荐
- ZOJ 3955 Saddle Point
排序. 枚举每一个格子,计算这个格子在多少矩阵中是鞍点,只要计算这一行有多少数字比他大,这一列有多少数字比他小,方案数乘一下就是这个格子对答案做出的贡献. #include<bits/stdc+ ...
- LongAdder & AtomicInteger
JDK8 推荐 LongAdder替代 AtomicInteger, AtomicInteger内部是实现使用 (网友使用jad反编译源码 参考 http://ifeve.com/enhanced- ...
- nyoj 737 石子合并 经典区间 dp
石子合并(一) 时间限制:1000 ms | 内存限制:65535 KB 难度:3 描述 有N堆石子排成一排,每堆石子有一定的数量.现要将N堆石子并成为一堆.合并的过程只能每次将相邻的两堆 ...
- [BZOJ4651][NOI2016]网格(Tarjan)
下面直接给出结论,相关证明见官方题解. 1.若跳蚤数不超过1或仅有两只跳蚤且相邻,则答案为-1. 2.若跳蚤形成的连通块个数大于1,则答案为0. 3.若跳蚤之间建图存在割点,则答案为1. 4.否则为2 ...
- 数位dp小结以及模板
这里是网址 别人的高一啊QAQ.... 嗯一般记忆化搜索是比递推好写的所以我写的都是dfs嗯......(因为我找不到规律啊摔,还是太菜.....) 显然这个东西的条件是非常的有套路..但是不管怎么样 ...
- 基于Java 生产者消费者模式(详细分析)
Java 生产者消费者模式详细分析 本文目录:1.等待.唤醒机制的原理2.Lock和Condition3.单生产者单消费者模式4.使用Lock和Condition实现单生产单消费模式5.多生产多消费模 ...
- 事务有哪些特性?spring的事务管理有几种方式实现,如何实现?
特性:1.原子性:一个事务中所有对数据库的操作是一个不可分割的操作序列,要么全做要么全不做 2.一致性:数据不会因为事务的执行而遭到破坏 3.隔离性:一个事物的执行,不受其他事务的干扰,即并发执行的事 ...
- iOS 反反注入 修改__RESTRICT,__restrict工具
通过在 Xcode 里的 Other Linker Flags 设置参数,可以防止App被注入dylib(仅限于iOS 10 以下系统) 比如,某艺,XX音乐等 dylib无法注入,也就意味着没办法 ...
- MySQL order by的一个优化思路
最近遇到一条SQL线上执行超过5s,这显然无法忍受了,必须要优化了. 首先看眼库表结构和SQL语句. CREATE TABLE `xxxxx` ( `id` ) NOT NULL AUTO_INCRE ...
- 二叉树遍历-JAVA实现
二叉树遍历分为前序.中序.后序递归和非递归遍历.还有层序遍历. //二叉树节点 public class BinaryTreeNode { private int data; private Bina ...