一天一道LeetCode系列

(一)题目

Implement atoi to convert a string to an integer.

Hint: Carefully consider all possible input cases. If you want a challenge, please do not see below and ask yourself what are the possible input cases.

Notes: It is intended for this problem to be specified vaguely (ie, no given input specs). You are responsible to gather all the input requirements up front.

Update (2015-02-10): The signature of the C++ function had been updated. If you still see your function signature accepts a const char* argument, please click the reload button to reset your code definition.

(二)解题

分析特殊情况:

1、字符前面有空格,“ 010”

2、数字前或者中间出现了其他的字符“b123”,“ -012b12123”

3、数字越界,int是-2147483648到2147483647

4、最奇葩的+-2,我觉得应该输出-2,可是leetcode上显示输出0。

class Solution {
public:
    int myAtoi(string str) {
        long lresult=0;
        int i = 0;
        int max=2147483647;
        int min=-2147483648;
        bool isNegetive  = false;
        while(str[i] != '\0' && str[i] ==' ') i++;//判断前面是空格的情况
        if(str[i] == '-') {isNegetive = true;i++;}//判断正负
        else if(str[i] == '+'){i++;}

        while(i<str.length())
        {
            if(str[i]>='0' && str[i]<='9')
            {
                lresult*=10;
                lresult+=(str[i] - '0');
                i++;
            }
            else break;//出现其他字符干扰则退出循环
            //检查越界情况
            long lresult_temp = (isNegetive == true) ?-lresult:lresult;
            if(!isNegetive&&lresult_temp>=max) return max;
            if(isNegetive&&lresult_temp<=min) return min;
        }
        return (isNegetive == true) ?-lresult:lresult;//输出结果

    }
};

感觉做这道题还是自己考虑的情况不够多,总是遗漏了一些特殊情况。

【一天一道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. android 调试工具ADB命令详解

    adb是什么? adb的全称为Android Debug Bridge,就是起到调试桥的作用. 通过adb我们可以在Eclipse中方面通过DDMS来调试Android程序,说白了就是debug工具. ...

  2. 用类模拟C风格的赋值+返回值

    这个方法比较好: class DataHolder: def __init__(self, value=None): self.value = value def set(self, value): ...

  3. Servlet - 基础

    Servlet 标签 : Java与Web HTTP协议 HTTP(hypertext transport protocol),即超文本传输协议.这个协议详细规定了浏览器(Browser)和万维网服务 ...

  4. 2 TileMapObject的使用

    1 CCTMXObjectGroup的使用方法 为了取以下内容: 操作代码如下: T27TileMapObject.h #ifndef __T27TileMapObject_H__ #define _ ...

  5. Afinal加载网络图片及下载文件使用方法

    Afinal快速开发框架使用起来非常方便,下面将讲解如何利用Afinal加载网络图片及下载文件: 先看效果图: 注意:使用Afinal前需添加Afinal的jar,可以在这里下载:http://dow ...

  6. CSDN发表文章后老是待审核的原因

    最近开始在csdn上写文章,发现老是文章被 待审核 ,于是便在网上搜集了下网友们的反馈,最后做出以下的整理. 1.CSDN检测到文章中的链接大于5,就会将文章列为"待审核",这个其 ...

  7. Effective C++ ——实现

    条款26:尽可能延后变量定义式的出现时间 当你定义一个变量的时候就要保证这个变量能够在程序中使用到,不要定义无意义的变量,这样就要求我们最好是在变量使用到的时候才做定义,因为如果一个变量定义了却不使用 ...

  8. DBCP连接池TestOnBorrow的坑

    生产环境连接池TestOnBorrow设置为false,导致有时获取的连接不可用.分析如下: TestOnBorrow=false时,由于不检测池里连接的可用性,于是假如连接池中的连接被数据库关闭了, ...

  9. Qt应用程序中设置字体

    Qt应用程序中设置字体 应用程序中经常需要设置字体,例如office软件或者是其他的编辑器软件等等.这里主要涉及到如下几个概念:字体,字号以及风格(例如:粗体,斜体,下划线等等).Qt里面也有对应的类 ...

  10. THE SCHOOLS WHERE APPLE, GOOGLE, AND FACEBOOK GET THEIR RECRUITS