一.题目链接: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)的更多相关文章

  1. Leetcode 8. String to Integer (atoi) atoi函数实现 (字符串)

    Leetcode 8. String to Integer (atoi) atoi函数实现 (字符串) 题目描述 实现atoi函数,将一个字符串转化为数字 测试样例 Input: "42&q ...

  2. leetcode day6 -- String to Integer (atoi) &amp;&amp; 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 ...

  3. 【leetcode】String to Integer (atoi)

    String to Integer (atoi) Implement atoi to convert a string to an integer. Hint: Carefully consider ...

  4. [leetcode] 8. String to Integer (atoi) (Medium)

    实现字符串转整形数字 遵循几个规则: 1. 函数首先丢弃尽可能多的空格字符,直到找到第一个非空格字符. 2. 此时取初始加号或减号. 3. 后面跟着尽可能多的数字,并将它们解释为一个数值. 4. 字符 ...

  5. Leetcode 8. String to Integer (atoi)(模拟题,水)

    8. String to Integer (atoi) Medium Implement atoi which converts a string to an integer. The functio ...

  6. [LeetCode][Python]String to Integer (atoi)

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com'https://oj.leetcode.com/problems/string- ...

  7. 【LeetCode】String to Integer (atoi) 解题报告

    这道题在LeetCode OJ上难道属于Easy.可是通过率却比較低,究其原因是须要考虑的情况比較低,非常少有人一遍过吧. [题目] Implement atoi to convert a strin ...

  8. LeetCode 8. String to Integer (atoi) (字符串到整数)

    Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. ...

  9. [LeetCode] 8. String to Integer (atoi) 字符串转为整数

    Implement atoi which converts a string to an integer. The function first discards as many whitespace ...

  10. LeetCode 7 -- String to Integer (atoi)

    Implement atoi to convert a string to an integer. 转换很简单,唯一的难点在于需要开率各种输入情况,例如空字符串,含有空格,字母等等. 另外需在写的时候 ...

随机推荐

  1. JavaBasic_02

    Java的基础框架 3W:What How Why What:一个东西是什么,具备什么样的功能 怎么用 How: 怎么做?功能如何实现 读源代码(jdk)->学习很多,优雅的编程技巧建立在wha ...

  2. CodeForces - 710F:String Set Queries (二进制分组 处理 在线AC自动机)

    ou should process m queries over a set D of strings. Each query is one of three kinds: Add a string ...

  3. matlab学习(1)strsplit与strtok

    strsplit函数用法: <1>默认使用空格符分割,返回一个cell数组 <2>也可以指定第二个参数进行分割 <3>第二个参数也可以时包含多个分隔符的元胞数组 & ...

  4. web前端的问题整理

    css实现三列布局?如果中间又是自适应布局怎么做?

  5. 用java的io流,将一个文本框的内容反转

    import java.io.*; import java.util.ArrayList; public class test04 { public static void main(String a ...

  6. vue中在页面渲染完之后获取元素(否则动态渲染的元素获取不到)

    两种方法: 方法一: 使用$nextTick,在异步获得数据之后再获取元素: 方法二: 在then之后再获取该元素: 问题2:vue中监听改变数组的方法: let idx =; this.listIn ...

  7. spring事务中出现oracle游标溢出的解决方案

    本例事务中大量查询SQL语句,会导致oracle游标溢出:对于数据库游标出现解决方案:1.大量查询SQL语句取消事务,只针对插入/更新 做事务处理2.用临时表代替大量查询SQL语句推荐使用第二种方案

  8. (惊艳)hashmap的理解(映射)

    第一: hashmap在内存中是长这样的,数组+链表的形式 // HashMap采用链表法解决冲突,每一个Entry本质上是一个单向链表 transient Entry[] table; 第二:  p ...

  9. shell excute mongo query command

    use shell command method one: #!/bin/bash ] then echo 'Please input cid' exit fi HOST= mongo ${HOST} ...

  10. centos 7.0 lnmp成功安装过程

    下载nginx,wget 是一个下载命令-c 是断点续传(不要也这个也可以) [root@bogon ~]# wget -c http://nginx.org/download/nginx-1.7.9 ...