String to Integer (atoi) leetcode

题目的意思是要将一个字符串转换成数字

这个题目的重点是要处理    各种各样的输入情况

在题目下面有一大段英文:

Requirements for atoi:

The function first discards as many whitespace characters as necessary until the first non-whitespace character is found. Then, starting from this character, takes an optional initial plus or minus sign followed
by as many numerical digits as possible, and interprets them as a numerical value.

The string can contain additional characters after those that form the integral number, which are ignored and have no effect on the behavior of this function.

If the first sequence of non-whitespace characters in str is not a valid integral number, or if no such sequence exists because either str is empty or it contains only whitespace characters, no conversion is performed.

If no valid conversion could be performed, a zero value is returned. If the correct value is out of the range of representable values, INT_MAX (2147483647) or INT_MIN (-2147483648) is returned.

上面其实提示了一些可能的输入或者需要注意的输入:

1、前面的空格符号     如  “_ _ _ 123”  前面的下划线表示空格    处理方法(忽略)

2、空格后面可能有正负号   正号我们不需要太关心 ,但是当正负号同时出现就错了   ,负号需要重点记录,因为后面还有溢出情况

3、正负号后面的 0    如    “+0000123”   处理方法(忽略)

4、其他字符  如果出现其他字符返回前面已经转换好的数字    如   “+000012abc45”  返回  12 ,如  “+abc123” 返回result=0   result初始值为0

5、溢出情况  需要保留负号是否出现情况

代码如下:

<span style="font-family:Arial;font-size:18px;">class Solution {
public:
    int atoi(const char *str) {

        if(str==NULL)
            return 0;

        while(*str==' ')  //表示忽略字符串前面出现的空格
            ++str;

        bool positive =false ;
        if(*str=='+')    //表示前面是否有正号
        {
            positive=true;
            ++str;
        }
        bool negative = false;//表示是否为负数,前面是否有负号
        if(*str=='-')
        {
            negative=true;
            ++str;
        }

        while(*str=='0')//消除正负号后面的 0
            ++str;

        if(negative&&positive) //表示正负号同时有  错误
            return 0;

        long long result=0;

        while(*str!='\0')
        {

            if(*str>='0'&&*str<='9')
            {
                result=result*10+*str-'0';

                if(negative==false&&result>INT_MAX) // 判断该数是不是大于最大数
                      return INT_MAX;

                if(negative==true&&-result<INT_MIN)  //判断该数是不是小于最小数
                    return INT_MIN;
            }
            else
            {
                break;
            }
            ++str;
        }
        if(negative==true)  //表示该数为负数
            return -result;
        if(negative==false) //表示该数为正数
            return result;
    }
};</span>
时间: 2024-12-25 07:25:27

String to Integer (atoi) leetcode的相关文章

String to Integer (atoi) leetcode java

题目: 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

8. String to Integer (atoi) Leetcode Python

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 spe

LeetCode:String to Integer (atoi)

1.题目名称 String to Integer (atoi) (字符串到数字的转换) 2.题目地址 https://leetcode.com/problems/string-to-integer-atoi/ 3.题目内容 英文:Implement atoi to convert a string to an integer. 中文:实现atoi函数,将输入的字符串(String类型)转换为整型数据(Integer类型) 提示:实现的atoi函数需要满足以下特征 忽略字符串第一个非空格字符前的所

Leetcode 数 String to Integer (atoi)

本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie String to Integer (atoi) Total Accepted: 9862 Total Submissions: 67880 Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. If you want a challenge,

LeetCode 008 String to Integer (atoi)

[题目] 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 b

leetcode——String to Integer (atoi) 字符串转换为整型数(AC)

mnesia在频繁操作数据的过程可能会报错:** WARNING ** Mnesia is overloaded: {dump_log, write_threshold},可以看出,mnesia应该是过载了.这个警告在mnesia dump操作会发生这个问题,表类型为disc_only_copies .disc_copies都可能会发生. 如何重现这个问题,例子的场景是多个进程同时在不断地mnesia:dirty_write/2 mnesia过载分析 1.抛出警告是在mnesia 增加dump

【leetcode系列】String to Integer (atoi)

这个我就直接上代码了,最开始把"abc123"也算作合法的了,后来查了一下atoi的定义,把这种去掉了. public class Solution { public static int atoi(String inStr) { long result = 0L; /* * 网上查了一下,atoi函数的定义是如果第一个非空格字符存在,是数字或者正负号则开始做类型转换, * 之后检测到非数字(包括结束符\0)字符时停止转换,返回整型数.否则,返回零.可能的输入情况有: 1.空字符串 *

【leetcode刷题笔记】String to Integer (atoi)

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 spe

LeetCode——String to Integer (atoi)

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 spe