LeetCode——8. String to Integer (atoi)
一.题目链接:https://leetcode.com/problems/string-to-integer-atoi/
二.题目大意:
实现一个和C语言里atoi具有相同功能的函数,即能够把字符串转换成整型。例如:输入字符串“-123”,返回值为一个整数-123。
三.题解:
这道题也属于字符串处理类的题目,只要看懂题目所给定的规则就好了,题目给的规则大致如下:
1.若一开始遇到连续的2空白字符,则忽略这些字符,直到遇到第一个非空白字符。
2.若遇到的第一个空白字符为'+'或者'-',则只有下一个字符为数字字符时才继续执行,否则返回0。如果下一个字符为数字字符,那么就往后顺序判断字符是否为数字字符,直到遇到非数字字符(然后该字符之后的字符串就不用考虑了)。
3.若遇到的第一个非空白字符为数字字符,则执行与第2步相同的操作,即往后顺序判断字符是否为数字字符,直到遇到非数字字符(然后该字符之后的字符串就不用考虑了)。(实际上相当于第一个出现的数字字符串)
4.如果数字字符串转换成的数字大于INT_MAX(int所能表示的最大值),或者小于INT_MIN(int所能表示的最小值)的话,则返回INT_MAX或者INT_MIN。
这里有需要注意的一点是:这道题不用考虑小数的问题,因为题目要求转化的是整型,不包括浮点型!!!
看懂这道题目给定的规则后,实现就简单了,代码如下:
class Solution
{
public:
int myAtoi(string str)
{
int len = str.size();
long long rs = 0;
int i = 0;
int flag = 1;//表示正负号
while(i < len && str[i] == 32)
{
i++;
}
if(str[i] == '+')
{
flag = 1;
i++;
while(str[i] >= '0' && str[i] <= '9')
{
rs = rs * 10 + str[i] - '0';
i++;
if(rs > INT_MAX)
break;
} }
else if(str[i] == '-')
{
flag = -1;
i++;
while(str[i] >= '0' && str[i] <= '9')
{
rs = rs * 10 + str[i] - '0';
i++;
if(rs > INT_MAX)
break;
}
}
else if(str[i] >= '0' && str[i] <= '9')
{
while(str[i] >= '0' && str[i] <= '9')
{
rs = rs * 10 + str[i] - '0';
i++;
if(rs > INT_MAX)
break;
}
}
else
return 0;
rs = rs *flag;
if(rs > INT_MAX)
return INT_MAX;
else if(rs < INT_MIN)
return INT_MIN;
else
return rs;
} };
该算法的时间复杂度为O(n),空间复杂度为O(1),这段代码有几处需要重点理解:
1.字符转数字的操作为:rs = rs * 10 + str[i] - '0'。此处实际上与第7题反转数字(http://www.cnblogs.com/wangkundentisy/p/8059028.html)用了相同的道理,这么做的好处是不用事先知道字符串的长度。
2.在while循环中有一步为:if(rs > INT_MAX) break;为什么要加这一步?最后的判断不能的代替这一步吗?
确实不能,因为rs 是long long型,这种数据类型占8个字节,它表示的范围也是有限的,所以当超过这个范围时,就会发生溢出,产生错误的返回值(例如:“1212121212121212121212121212121212121212”)。所以在字符串转化的过程中,一旦发现超过int的范围,就终止,就不会发生这种情况了。(之前我想的是把所有的rs都加完之后再考虑,这样就有可能发生上述情况,所以在rs增加的过程中判断才是比较合理的)。
LeetCode——8. String to Integer (atoi)的更多相关文章
- Leetcode 8. String to Integer (atoi) atoi函数实现 (字符串)
Leetcode 8. String to Integer (atoi) atoi函数实现 (字符串) 题目描述 实现atoi函数,将一个字符串转化为数字 测试样例 Input: "42&q ...
- leetcode day6 -- String to Integer (atoi) && Best Time to Buy and Sell Stock I II III
1. String to Integer (atoi) Implement atoi to convert a string to an integer. Hint: Carefully con ...
- 【leetcode】String to Integer (atoi)
String to Integer (atoi) Implement atoi to convert a string to an integer. Hint: Carefully consider ...
- [leetcode] 8. String to Integer (atoi) (Medium)
实现字符串转整形数字 遵循几个规则: 1. 函数首先丢弃尽可能多的空格字符,直到找到第一个非空格字符. 2. 此时取初始加号或减号. 3. 后面跟着尽可能多的数字,并将它们解释为一个数值. 4. 字符 ...
- Leetcode 8. String to Integer (atoi)(模拟题,水)
8. String to Integer (atoi) Medium Implement atoi which converts a string to an integer. The functio ...
- [LeetCode][Python]String to Integer (atoi)
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com'https://oj.leetcode.com/problems/string- ...
- 【LeetCode】String to Integer (atoi) 解题报告
这道题在LeetCode OJ上难道属于Easy.可是通过率却比較低,究其原因是须要考虑的情况比較低,非常少有人一遍过吧. [题目] Implement atoi to convert a strin ...
- LeetCode 8. String to Integer (atoi) (字符串到整数)
Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. ...
- [LeetCode] 8. String to Integer (atoi) 字符串转为整数
Implement atoi which converts a string to an integer. The function first discards as many whitespace ...
- LeetCode 7 -- String to Integer (atoi)
Implement atoi to convert a string to an integer. 转换很简单,唯一的难点在于需要开率各种输入情况,例如空字符串,含有空格,字母等等. 另外需在写的时候 ...
随机推荐
- NOI-1.8-17-最好的草-矩阵找最大连接井号-【递归】
17:最好的草 查看 提交 统计 提问 总时间限制: 10000ms 单个测试点时间限制: 1000ms 内存限制: 65536kB 描述 奶牛Bessie计划好好享受柔软的春季新草.新草分布在 ...
- day04之流程控制
if语句: if 条件1: pass elif 条件2: pass elif 条件3: pass else: pass if 条件语句中,先判断条件1,如果满足条件1,则执行第二行代码,第二行执行完后 ...
- eclipse如何为java项目生成API文档、JavaDoc
当我们的项目很大,编写了很多代码的时候,就需要生成一个标准的API文档,让后续的开发人员,或者合作者可以清晰的了解您方法的使用,那么如何将自己的项目生成API文档呢? 1.点击eclipse的[Pro ...
- HDU 2058:The sum problem(数学)
The sum problem Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- 使用w uptime vmstat top sar nload 等命令查看系统负载
1. w 和uptime,查看cpu的使用率: 2.vmstat 命令,查看更细的物理设备使用状况: 3.top 命令: top -c 可具体查看命令及路径: top -bn1 静太显示一条命令, ...
- djangorestful framework (三)学习
十.分页器 -简单分页: -from rest_framework.pagination import PageNumberPagination -生成一个对象 -调用对象的.page.paginat ...
- Go Example--缓存通道
package main import "fmt" func main() { //缓存通道 msg := make(chan string,2) msg <- " ...
- Go Example--if语句
package main import "fmt" func main() { //if else 条件都不需要括号,{}是需要的 if 7 % 2 == 0 { fmt.Prin ...
- test20190331
贪吃蛇(snaker.cpp/.in.out 1S 512M) [题目描述] C 最近爱上了玩贪吃蛇,想要自己制作一款贪吃蛇游戏,但是她太弱了,只会写一个一维的程序. 游戏界面可以看做一条长为 n 的 ...
- Full Schema Stitching with Apollo Server
转自: https://tomasalabes.me/blog/nodejs/graphql/apollo/2018/09/18/schema-stitiching-apollo.html Full ...