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. 转换很简单,唯一的难点在于需要开率各种输入情况,例如空字符串,含有空格,字母等等. 另外需在写的时候 ...
随机推荐
- 基于hiredis,redis C客户端封装
项目中需要用到redis就封装了一下,基于hiredis,只封装了string和哈希的部分方法.编译时加入-D__USER_LOCK__添加线程安全. suntelRedisCli.h #ifndef ...
- SEO : 建站注意
1.url格式.尽可能的短一些,实践证明,较短的url格式还是比较利于搜索引擎收录的. 2.网站前台要纯静态.虽然搜索引擎对静态页面和动态页面并没有本质上的差别对待,但是实践告诉我们静态页面对服务器的 ...
- Linux安装模式AppImage,Flatpak,Snap整理
本文只谈Linux世界用户较多的前2大主要分支, RedHat Red Hat Enterprise Linux 简称RHEL rpm (RedHat, CentOS, Fedora, Oracle. ...
- (0)diango、ORM的语法
Django PS:Django默认的是sqlite3数据库 PS:settings里面的这里可以修改数据库 1.^ 这个符号就是以什么开头 #url(r'index/',views.index) ...
- BZOJ3730 震波 和 BZOJ4372 烁烁的游戏
"震波"题意 F.A.Qs Home Discuss ProblemSet Status Ranklist Contest 入门OJ ModifyUser autoint Log ...
- 从MySQL和MongoDB的对比,看SQL与NoSQL的较量
张家江,网易乐得高级工程师. 贵金属(注:贵金属为笔者部门业务)的行情系统提供的接口通过Redis获取数据,目前使用Redis最多只存储了大概8000条左右的分钟k的行情数据,考虑到将来可能会有更大数 ...
- Redis怎么保持缓存与数据库一致性?
将不一致分为三种情况: 1. 数据库有数据,缓存没有数据: 2. 数据库有数据,缓存也有数据,数据不相等: 3. 数据库没有数据,缓存有数据. 在讨论这三种情况之前,先说明一下我使用缓存的策略,也是大 ...
- mysqldump命令之常用选项
===============================================mysqldump常用选项-h, --host=name:服务器IP-u, --user=name:登录名 ...
- oracle-闪回技术1
http://blog.csdn.net/lqx0405/article/details/44776737 Oracle Study案例之--基于表空间的时间点恢复(TSPITR) DBPITR 数 ...
- Day 38 HTML
HTML文档结构 <!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset=&qu ...