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. 转换很简单,唯一的难点在于需要开率各种输入情况,例如空字符串,含有空格,字母等等. 另外需在写的时候 ...
随机推荐
- Scrapy对接selenium+phantomjs
1.创建项目 :Jd 2.middlewares.py中添加selenium 1.导模块 :from selenium import webdriver 2.定义中间件 class seleniumM ...
- 初学html的单词笔记
font-size: 文字大小color: 顏色solid: 边框线text-align: 間距center: 文字放在中間<head> 网页头部<title> 网页标题< ...
- doc四则运算
import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; import java.ut ...
- 转载《Oracle的tnsnames.ora配置(PLSQL Developer)》
源地址:https://www.cnblogs.com/qq3245792286/p/6212617.html. 首先打开tnsnames.ora的存放目录,一般为D:\app\Administrat ...
- String的方法capitalize
官方解释:Return a copy of the string with its first character capitalized and the rest lowercased.(返回字符串 ...
- itcast-spring-三大框架整合
三大框架架构(整合原理) struts整合到spring hibernate整合到spring 导包 eclipse需要导入 myeclipse不用 单独配置spring容器 单独配置stru ...
- 浮动IP(Floating IPs):开始构建你的高可用性的应用
高可用性是所有生产环境的关键.开发者因此可以高枕无忧因为他们知道他们的应用被设计为可以承受住故障. 今天,我们非常激动的宣布我们应用了浮动IP技术.浮动IP指的是一个IP地址可以立即从一个Drople ...
- word怎么在方框中打对号
最快最简单的方法,是在word里输入一个大写的R,然后选中并将字体改为wingdings2,至于那个带叉号的方框图形,可以输入大写字母T并将字体设置为windings2
- mysql新建用户在本地无法登录
新建了一个mysql用户,但是无法在本地登录,即使已经授权任一ip都可以登录,甚至特地写清楚localhost登录,还是不行,情况如下 [root@localhost zabbix-release-3 ...
- 使用C语言简单模拟Linux的cat程序
先给出源码 //fileio.c #include<stdio.h> #include<stdlib.h> #include<fcntl.h> void print ...