一天一道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. Programming In Scala笔记-第七章、Scala中的控制结构

    所谓的内建控制结构是指编程语言中可以使用的一些代码控制语法,如Scala中的if, while, for, try, match, 以及函数调用等.需要注意的是,Scala几乎所有的内建控制结构都会返 ...

  2. Programming In Scala笔记-第六章、函数式对象

    这一章主要是以定义和完善一个有理数类Rational为线索,分析和介绍有关类定义,构造函数,方法重写,变量定义和私有化,以及对操作符的定义等. 一.Rational类定义和构造函数 1.定义一个空类 ...

  3. CountDownLatch使用

    分享牛原创,CountDownLatch类的使用,CountDownLatch是一个工具类,运行主线程开启子线程的时候,子线程还没有结束的时候,主线程可以一直等待,直到初始化的现成的计数器count为 ...

  4. Android Multimedia框架总结(十七)音频开发基础知识

    请尊重分享成果,转载请注明出处,本文来自逆流的鱼yuiop,原文链接:http://blog.csdn.net/hejjunlin/article/details/53078828 近年来,唱吧,全民 ...

  5. ROS探索总结(十八)——重读tf

    在之前的博客中,有讲解tf的相关内容,本篇博客重新整理了tf的介绍和学习内容,对tf的认识会更加系统. 1 tf简介 1.1 什么是tf tf是一个让用户随时间跟踪多个参考系的功能包,它使用一种树型数 ...

  6. 给定一个实数数组,按序排列(从小到大),从数组从找出若干个数,使得这若干个数的和与M最为接近,描述一个算法,并给出算法的复杂度。

    有N个正实数(注意是实数,大小升序排列) x1 , x2 ... xN,另有一个实数M. 需要选出若干个x,使这几个x的和与 M 最接近. 请描述实现算法,并指出算法复杂度. #define M 8 ...

  7. 详解EBS接口开发之库存事务处理-物料批次导入

    库存事务处理-物料批次导入 --系统批次表 SELECT * FROM MTL_LOT_NUMBERS T; --API创建批次 inv_lot_api_pub.create_inv_lot(x_re ...

  8. activiti节点跳转

    分享牛原创(尊重原创 转载对的时候第一行请注明,转载出处来自分享牛http://blog.csdn.net/qq_30739519) activiti使用的时候,通常需要跟业务紧密的结合在一起,有些业 ...

  9. 20 ViewPager Demo4自动轮播

    MainActivity.java 思想:才用非常大的数 让其看起来可以循环轮播图片并且用户可以从尽头滑到首图的特点 . package com.qf.day20_viewpager_demo4; i ...

  10. memcached实战系列(三)memcached命令使用

    memcached命令的使用,在这里我们最好了解一下命令的含义,对命令有一个大致的了解,在了解的基础上进行使用.这里的命名是常用的crud命令的演示. 1.1.1. memcached命令的格式 标准 ...